Skip to content

Commit c6b1387

Browse files
authored
Merge pull request #17 from friendsofgo/access_control_CORS
Fix CORS add AccessControl allowing methods and headers
2 parents b9b49fb + 3df77dd commit c6b1387

File tree

4 files changed

+42
-21
lines changed

4 files changed

+42
-21
lines changed

CHANGELOG.md

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,12 @@
11
# Changelog
22

3-
## v0.1.0 (2019/04/21)
4-
5-
* Add Killgrave logo
6-
* Add CircleCI integration
7-
* Convert headers into canonical mime type
8-
* Run server with imposter configuration
9-
* Processing and parsing imposters file
10-
* Initial version
3+
## v0.3.2 (2019/05/08)
114

12-
## v0.2.0 (2019/04/24)
13-
14-
* Create an official docker image for the application
15-
* Update README.md with how to use the application with docker
16-
* Allow write headers for the response
5+
* Fix CORS add AccessControl allowing methods and headers
176

18-
## v0.2.1 (2019/04/25)
7+
## v0.3.1 (2019/05/07)
198

20-
* Allow imposter's matching by request schema
21-
* Dynamic responses based on regex endpoint or request schema
22-
* Calculate files directory(body and schema) based on imposters path
23-
* Update REAMDE.md with resolved features and new future features
9+
* Allow CORS requests
2410

2511
## v0.3.0 (2019/04/28)
2612

@@ -31,6 +17,24 @@
3117
* Allow organize your imposters with structured folders (using new extension `.imp.json`)
3218
* Allow write multiple imposters by file
3319

34-
## v0.3.1 (2019/05/07)
20+
## v0.2.1 (2019/04/25)
3521

36-
* Allow CORS requests
22+
* Allow imposter's matching by request schema
23+
* Dynamic responses based on regex endpoint or request schema
24+
* Calculate files directory(body and schema) based on imposters path
25+
* Update REAMDE.md with resolved features and new future features
26+
27+
## v0.2.0 (2019/04/24)
28+
29+
* Create an official docker image for the application
30+
* Update README.md with how to use the application with docker
31+
* Allow write headers for the response
32+
33+
## v0.1.0 (2019/04/21)
34+
35+
* Add Killgrave logo
36+
* Add CircleCI integration
37+
* Convert headers into canonical mime type
38+
* Run server with imposter configuration
39+
* Processing and parsing imposters file
40+
* Initial version

cmd/killgrave/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@ func main() {
3737

3838
httpAddr := fmt.Sprintf("%s:%d", *host, *port)
3939
log.Printf("The fake server is on tap now: http://%s:%d\n", *host, *port)
40-
log.Fatal(http.ListenAndServe(httpAddr, handlers.CORS()(r)))
40+
log.Fatal(http.ListenAndServe(httpAddr, handlers.CORS(s.AccessControl()...)(r)))
4141
}

internal/server.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"path/filepath"
99

10+
"github.com/gorilla/handlers"
1011
"github.com/gorilla/mux"
1112
"github.com/pkg/errors"
1213
)
@@ -25,6 +26,13 @@ func NewServer(p string, r *mux.Router) *Server {
2526
}
2627
}
2728

29+
// AccessControl Return options to initialize the mock server with default access control
30+
func (s *Server) AccessControl() (h []handlers.CORSOption) {
31+
h = append(h, handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS", "DELETE", "PATCH", "TRACE", "CONNECT"}))
32+
h = append(h, handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type", "*"}))
33+
return
34+
}
35+
2836
// Build read all the files on the impostersPath and add different
2937
// handlers for each imposter
3038
func (s *Server) Build() error {

internal/server_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,12 @@ func TestRunServer(t *testing.T) {
3636
})
3737
}
3838
}
39+
40+
func TestAccessControl(t *testing.T) {
41+
s := NewServer("test/testdata/imposters", mux.NewRouter())
42+
h := s.AccessControl()
43+
44+
if len(h) <= 0 {
45+
t.Fatal("Expected any CORS options and got empty")
46+
}
47+
}

0 commit comments

Comments
 (0)