Skip to content

Commit

Permalink
Adding moar tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dynom committed Jul 11, 2018
1 parent 9244d7f commit 28e16df
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
37 changes: 37 additions & 0 deletions server/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"bytes"
"encoding/json"

"strings"

"github.com/sirupsen/logrus/hooks/test"
)

Expand Down Expand Up @@ -106,3 +108,38 @@ func TestRequestID(t *testing.T) {
t.Errorf("Did not expect the request ID to be empty.")
}
}

func TestCORS(t *testing.T) {
recorder := httptest.NewRecorder()

reqOrigin := "https://example.org"
reqMethod := http.MethodPost
reqHeader := "X-Foo-Bar"

{ // setup
req := httptest.NewRequest(http.MethodOptions, "/", nil)
req.Header.Set("Origin", reqOrigin)
req.Header.Set("Access-Control-Request-Method", reqMethod)
req.Header.Set("Access-Control-Request-Headers", reqHeader)

c := createCORSType(nil)
c.Handler(http.NotFoundHandler()).ServeHTTP(recorder, req)
}

resultOrigin := recorder.Result().Header.Get("Access-Control-Allow-Origin")
resultMethods := recorder.Result().Header.Get("Access-Control-Allow-Methods")
resultHeaders := recorder.Result().Header.Get("Access-Control-Allow-Headers")

if resultOrigin != reqOrigin {
t.Errorf("Expected the origins to match")
t.Logf("%+v", recorder.Result())
}

if !strings.Contains(resultHeaders, reqHeader) {
t.Errorf("Expected the headers to be present")
}

if !strings.Contains(resultMethods, reqMethod) {
t.Errorf("Expected the methods to be present")
}
}
12 changes: 9 additions & 3 deletions server/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,22 @@ func WithLogger(logger *logrus.Logger) Option {

// WithCORS adds the CORS handler to the request handling
func WithCORS(allowedOrigins []string) Option {
c := createCORSType(allowedOrigins)

return func(server *TySugServer) {
server.handlers = append(server.handlers, c.Handler)
}
}

func createCORSType(allowedOrigins []string) *cors.Cors {
c := cors.New(cors.Options{
AllowCredentials: true,
AllowedHeaders: []string{"*"},
AllowedMethods: []string{http.MethodPost},
AllowedOrigins: allowedOrigins,
})

return func(server *TySugServer) {
server.handlers = append(server.handlers, c.Handler)
}
return c
}

// WithInputLimitValidator specifies a max input-value limit validator
Expand Down
14 changes: 14 additions & 0 deletions server/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ func TestWithInputLimitValidator(t *testing.T) {
if err := v(req); err != nil {
t.Errorf("Expected the request to be valid, since the input is less than 12 bytes in size %+v", err)
}

req = tySugRequest{Input: ""}
if err := v(req); err == nil {
t.Errorf("Expected the request to be invalid, since the input is empty %+v", err)
}
}

func TestWithCORSOneOrigin(t *testing.T) {
Expand All @@ -38,3 +43,12 @@ func TestWithCORSOneOrigin(t *testing.T) {
t.Errorf("Expected exactly one handler, instead I got %d.", l)
}
}

func TestWithLogger(t *testing.T) {
s := TySugServer{}

WithLogger(nil)(&s)
if s.Logger == nil {
t.Errorf("Expected a default logger to be defined, if none was specified")
}
}

0 comments on commit 28e16df

Please sign in to comment.