Skip to content

Commit

Permalink
Merge pull request #17 from friendsofgo/access_control_CORS
Browse files Browse the repository at this point in the history
Fix CORS add AccessControl allowing methods and headers
  • Loading branch information
aperezg authored May 8, 2019
2 parents b9b49fb + 3df77dd commit c6b1387
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 21 deletions.
44 changes: 24 additions & 20 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
# Changelog

## v0.1.0 (2019/04/21)

* Add Killgrave logo
* Add CircleCI integration
* Convert headers into canonical mime type
* Run server with imposter configuration
* Processing and parsing imposters file
* Initial version
## v0.3.2 (2019/05/08)

## v0.2.0 (2019/04/24)

* Create an official docker image for the application
* Update README.md with how to use the application with docker
* Allow write headers for the response
* Fix CORS add AccessControl allowing methods and headers

## v0.2.1 (2019/04/25)
## v0.3.1 (2019/05/07)

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

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

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

## v0.3.1 (2019/05/07)
## v0.2.1 (2019/04/25)

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

## v0.2.0 (2019/04/24)

* Create an official docker image for the application
* Update README.md with how to use the application with docker
* Allow write headers for the response

## v0.1.0 (2019/04/21)

* Add Killgrave logo
* Add CircleCI integration
* Convert headers into canonical mime type
* Run server with imposter configuration
* Processing and parsing imposters file
* Initial version
2 changes: 1 addition & 1 deletion cmd/killgrave/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ func main() {

httpAddr := fmt.Sprintf("%s:%d", *host, *port)
log.Printf("The fake server is on tap now: http://%s:%d\n", *host, *port)
log.Fatal(http.ListenAndServe(httpAddr, handlers.CORS()(r)))
log.Fatal(http.ListenAndServe(httpAddr, handlers.CORS(s.AccessControl()...)(r)))
}
8 changes: 8 additions & 0 deletions internal/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"

"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/pkg/errors"
)
Expand All @@ -25,6 +26,13 @@ func NewServer(p string, r *mux.Router) *Server {
}
}

// AccessControl Return options to initialize the mock server with default access control
func (s *Server) AccessControl() (h []handlers.CORSOption) {
h = append(h, handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS", "DELETE", "PATCH", "TRACE", "CONNECT"}))
h = append(h, handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type", "*"}))
return
}

// Build read all the files on the impostersPath and add different
// handlers for each imposter
func (s *Server) Build() error {
Expand Down
9 changes: 9 additions & 0 deletions internal/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,12 @@ func TestRunServer(t *testing.T) {
})
}
}

func TestAccessControl(t *testing.T) {
s := NewServer("test/testdata/imposters", mux.NewRouter())
h := s.AccessControl()

if len(h) <= 0 {
t.Fatal("Expected any CORS options and got empty")
}
}

0 comments on commit c6b1387

Please sign in to comment.