From 745a5acc3cfbde1109b0ffdfc6214b356cacac76 Mon Sep 17 00:00:00 2001 From: bodhi Date: Fri, 8 Sep 2017 15:28:54 +1000 Subject: [PATCH 1/2] Add CORS middleware to allow browser requests --- example/examplemain/main.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/example/examplemain/main.go b/example/examplemain/main.go index 2925a2b..d0c7d44 100644 --- a/example/examplemain/main.go +++ b/example/examplemain/main.go @@ -4,6 +4,7 @@ import ( "fmt" "net/http" + "github.com/rs/cors" "github.com/theplant/appkit/log" "github.com/theplant/appkit/server" "github.com/theplant/prottp/example" @@ -14,7 +15,13 @@ func main() { mux := http.NewServeMux() example.Mount(mux) l := log.Default() + + c := cors.New(cors.Options{ + AllowedOrigins: []string{"*"}, + }) + defaultmws := server.Compose( + c.Handler, server.DefaultMiddleware(l), ) From ad94dc056888c55f66664f9cabdcacd3b1d4ff8a Mon Sep 17 00:00:00 2001 From: bodhi Date: Fri, 8 Sep 2017 15:34:49 +1000 Subject: [PATCH 2/2] Fix test cases that expect exact JSON syntax The fix is just as brittle as the original test. It should compare the semantic JSON message, not the exact syntax, eg. "{}" is the same JSON as " { \n \t }" --- example/example_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/example/example_test.go b/example/example_test.go index 5457870..3c20e44 100644 --- a/example/example_test.go +++ b/example/example_test.go @@ -36,8 +36,7 @@ var cases = []struct { "page_number": 1, "result_per_page": 10 }`, - ExpectedJSONResBody: ` -{ + ExpectedJSONResBody: `{ "result": [ { "url": "query string", @@ -72,8 +71,7 @@ var cases = []struct { "result_per_page": 10 }`, ExpectedStatusCode: 500, - ExpectedJSONResBody: ` -{ + ExpectedJSONResBody: `{ "field": "field123", "error_count": 100 }`, @@ -86,8 +84,10 @@ var cases = []struct { "page_number": 1, "result_per_page": 10] }`, - ExpectedStatusCode: 400, - ExpectedJSONResBody: `{}`, + ExpectedStatusCode: 400, + ExpectedJSONResBody: `{ + +}`, }, { Name: "test response nil should panic",