-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
156 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ package composition | |
import ( | ||
gomock "github.com/golang/mock/gomock" | ||
io "io" | ||
|
||
http "net/http" | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Automatically generated by MockGen. DO NOT EDIT! | ||
// Source: net/http (interfaces: Handler) | ||
|
||
package http | ||
|
||
import ( | ||
gomock "github.com/golang/mock/gomock" | ||
http "net/http" | ||
) | ||
|
||
// Mock of Handler interface | ||
type MockHandler struct { | ||
ctrl *gomock.Controller | ||
recorder *_MockHandlerRecorder | ||
} | ||
|
||
// Recorder for MockHandler (not exported) | ||
type _MockHandlerRecorder struct { | ||
mock *MockHandler | ||
} | ||
|
||
func NewMockHandler(ctrl *gomock.Controller) *MockHandler { | ||
mock := &MockHandler{ctrl: ctrl} | ||
mock.recorder = &_MockHandlerRecorder{mock} | ||
return mock | ||
} | ||
|
||
func (_m *MockHandler) EXPECT() *_MockHandlerRecorder { | ||
return _m.recorder | ||
} | ||
|
||
func (_m *MockHandler) ServeHTTP(_param0 http.ResponseWriter, _param1 *http.Request) { | ||
_m.ctrl.Call(_m, "ServeHTTP", _param0, _param1) | ||
} | ||
|
||
func (_mr *_MockHandlerRecorder) ServeHTTP(arg0, arg1 interface{}) *gomock.Call { | ||
return _mr.mock.ctrl.RecordCall(_mr.mock, "ServeHTTP", arg0, arg1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/bin/bash | ||
|
||
# should be in your path | ||
MOCKGEN=mockgen | ||
SED=sed | ||
GOFMT=gofmt | ||
MKDIR=mkdir | ||
|
||
generate_mock() { | ||
SRC=$1 | ||
PKG=$(dirname $SRC) | ||
DST=$PKG/mock_$(basename $SRC) | ||
|
||
$MKDIR -p $(dirname $DST) | ||
$MOCKGEN -source ./$SRC -destination ./$DST -package $(basename $PKG) | ||
$GOFMT -w ./$DST | ||
} | ||
|
||
generate_vendor_mock() { | ||
PKG=$1 | ||
INTERFACES=$2 | ||
DST=mocks/$PKG/mock_$(basename $PKG).go | ||
|
||
$MKDIR -p $(dirname $DST) | ||
$MOCKGEN -destination ./$DST -package $(basename $PKG) $PKG $INTERFACES | ||
$GOFMT -w ./$DST | ||
} | ||
|
||
# generate vendor mocks | ||
generate_vendor_mock net/http Handler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package util | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
const X_FORWARDED_HOST_HEADER_KEY = "X-Forwarded-Host" | ||
|
||
type ForwardedHostHandler struct { | ||
Next http.Handler | ||
} | ||
|
||
func NewForwardedHostHandler(next http.Handler) *ForwardedHostHandler { | ||
return &ForwardedHostHandler{Next: next} | ||
} | ||
|
||
func (p *ForwardedHostHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
r.Header.Set(X_FORWARDED_HOST_HEADER_KEY, r.Host) | ||
|
||
if p.Next != nil { | ||
p.Next.ServeHTTP(w, r) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package util | ||
|
||
import ( | ||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/assert" | ||
mockHttp "github.com/tarent/lib-compose/composition/mocks/net/http" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func Test_DelegateIsCalled(t *testing.T) { | ||
ctl := gomock.NewController(t) | ||
defer ctl.Finish() | ||
|
||
mockDelegator := mockHttp.NewMockHandler(ctl) | ||
fhh := NewForwardedHostHandler(mockDelegator) | ||
|
||
req, _ := http.NewRequest("GET", "", nil) | ||
resp := httptest.NewRecorder() | ||
|
||
//expected the delegate should not been called | ||
mockDelegator.EXPECT().ServeHTTP(gomock.Any(), gomock.Any()).Times(1) | ||
|
||
// when | ||
fhh.ServeHTTP(resp, req) | ||
} | ||
|
||
func Test_XForwardeHostHeaderIsSet(t *testing.T) { | ||
ctl := gomock.NewController(t) | ||
defer ctl.Finish() | ||
|
||
fhh := ForwardedHostHandler{} | ||
|
||
req, _ := http.NewRequest("GET", "", nil) | ||
|
||
expected := "testitest" | ||
req.Host = expected | ||
resp := httptest.NewRecorder() | ||
|
||
//when | ||
fhh.ServeHTTP(resp, req) | ||
assert.Contains(t, req.Header.Get(X_FORWARDED_HOST_HEADER_KEY), expected) | ||
} | ||
|
||
func Test_OldXForwardedHostHeaderIsDropped(t *testing.T) { | ||
ctl := gomock.NewController(t) | ||
defer ctl.Finish() | ||
|
||
fhh := ForwardedHostHandler{} | ||
|
||
req, _ := http.NewRequest("GET", "", nil) | ||
|
||
notExpected := "nottestitest" | ||
req.Header.Set(X_FORWARDED_HOST_HEADER_KEY, notExpected) | ||
resp := httptest.NewRecorder() | ||
|
||
//when | ||
fhh.ServeHTTP(resp, req) | ||
assert.Equal(t, req.Header.Get(X_FORWARDED_HOST_HEADER_KEY), "") | ||
} |