Skip to content

Commit ddb9236

Browse files
committed
init: first init
* first init
0 parents  commit ddb9236

File tree

6 files changed

+105
-0
lines changed

6 files changed

+105
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 YOSGO <https://yosgo.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# HTTP(s) middleware go

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/yosgo-opensource/http-middleware-go
2+
3+
go 1.16
4+
5+
require github.com/stretchr/testify v1.7.0

go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
6+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
7+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
9+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
10+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
11+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

middleware.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package middleware
2+
3+
import (
4+
"net/http"
5+
)
6+
7+
func httpJSONResponse(next http.HandlerFunc) http.HandlerFunc {
8+
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
9+
switch r.Method {
10+
case http.MethodGet:
11+
rw.Header().Set("Content-Type", "text/html")
12+
case http.MethodPost:
13+
rw.Header().Set("Content-Type", "application/json")
14+
}
15+
16+
next.ServeHTTP(rw, r)
17+
})
18+
}
19+
20+
func httpCORS(next http.HandlerFunc) http.HandlerFunc {
21+
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
22+
rw.Header().Set("Access-Control-Allow-Origin", "*")
23+
rw.Header().Set("Access-Control-Allow-Headers", "Content-Type")
24+
25+
next.ServeHTTP(rw, r)
26+
})
27+
}

middleware_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package middleware
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
type MockMux struct{}
12+
13+
func ServeHTTP(rw http.ResponseWriter, r *http.Request) {}
14+
15+
func TestHTTPCORSHeaders(t *testing.T) {
16+
assert := assert.New(t)
17+
18+
req := httptest.NewRequest(http.MethodPost, "/_watermark", nil)
19+
w := httptest.NewRecorder()
20+
21+
cors := httpCORS(ServeHTTP)
22+
cors(w, req)
23+
24+
res := w.Result()
25+
assert.Equal(res.Header.Get("Access-Control-Allow-Origin"), "*")
26+
assert.Equal(res.Header.Get("Access-Control-Allow-Headers"), "Content-Type")
27+
}
28+
29+
func TestJSONResponseHeaders(t *testing.T) {
30+
assert := assert.New(t)
31+
32+
req := httptest.NewRequest(http.MethodPost, "/_watermark", nil)
33+
w := httptest.NewRecorder()
34+
35+
cors := httpJSONResponse(ServeHTTP)
36+
cors(w, req)
37+
38+
res := w.Result()
39+
assert.Equal(res.Header.Get("Content-Type"), "application/json")
40+
}

0 commit comments

Comments
 (0)