-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement enable middleware tripperware (#29)
- Loading branch information
1 parent
c7e09db
commit 7947cd4
Showing
14 changed files
with
260 additions
and
6 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
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,14 @@ | ||
package middleware | ||
|
||
import ( | ||
"github.com/gol4ng/httpware/v2" | ||
) | ||
|
||
// Enable middleware is used to conditionnaly add a middleware to a MiddlewareStack | ||
// See Skip middleware to active a middleware in function of request | ||
func Enable(enable bool, middleware httpware.Middleware) httpware.Middleware { | ||
if enable { | ||
return middleware | ||
} | ||
return httpware.NopMiddleware | ||
} |
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,89 @@ | ||
package middleware_test | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/gol4ng/httpware/v2" | ||
"github.com/gol4ng/httpware/v2/middleware" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestEnable(t *testing.T) { | ||
tests := []struct { | ||
enable bool | ||
expectedExecuted bool | ||
}{ | ||
{ | ||
enable: true, | ||
expectedExecuted: true, | ||
}, | ||
{ | ||
enable: false, | ||
expectedExecuted: false, | ||
}, | ||
} | ||
|
||
request := httptest.NewRequest(http.MethodGet, "http://fake-addr", nil) | ||
responseWriter := &httptest.ResponseRecorder{} | ||
|
||
handler := http.HandlerFunc(func(writer http.ResponseWriter, req *http.Request) { | ||
assert.Equal(t, request, req) | ||
writer.WriteHeader(http.StatusOK) | ||
}) | ||
|
||
executed := false | ||
dummyMiddleware := func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { | ||
executed = true | ||
next.ServeHTTP(writer, request) | ||
}) | ||
} | ||
|
||
for k, test := range tests { | ||
executed = false | ||
t.Run(fmt.Sprintf("test %d (%v)", k, test), func(t *testing.T) { | ||
middleware.Enable(test.enable, dummyMiddleware)(handler).ServeHTTP(responseWriter, request) | ||
|
||
assert.Equal(t, test.expectedExecuted, executed) | ||
}) | ||
} | ||
} | ||
|
||
// ===================================================================================================================== | ||
// ========================================= EXAMPLES ================================================================== | ||
// ===================================================================================================================== | ||
|
||
func ExampleEnable() { | ||
port := ":9104" | ||
|
||
enableDummyMiddleware := true // or false | ||
dummyMiddleware := func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { | ||
request.Header.Set("FakeHeader", "this header is set when not /home url") | ||
next.ServeHTTP(writer, request) | ||
}) | ||
} | ||
stack := httpware.MiddlewareStack( | ||
middleware.Enable(enableDummyMiddleware, dummyMiddleware), | ||
) | ||
|
||
// create a server in order to show it work | ||
srv := http.NewServeMux() | ||
srv.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { | ||
fmt.Println("server receive request with request:", request.Header.Get("FakeHeader")) | ||
}) | ||
|
||
go func() { | ||
if err := http.ListenAndServe(port, stack.DecorateHandler(srv)); err != nil { | ||
panic(err) | ||
} | ||
}() | ||
|
||
_, _ = http.Get("http://localhost" + port + "/") | ||
|
||
// Output: | ||
//server receive request with request: this header is set when not /home url | ||
} |
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
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
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,14 @@ | ||
package tripperware | ||
|
||
import ( | ||
"github.com/gol4ng/httpware/v2" | ||
) | ||
|
||
// Enable tripperware is used to conditionnaly add a tripperware to a TripperwareStack | ||
// See Skip tripperware to active a tripperware in function of request | ||
func Enable(enable bool, tripperware httpware.Tripperware) httpware.Tripperware { | ||
if enable { | ||
return tripperware | ||
} | ||
return httpware.NopTripperware | ||
} |
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,95 @@ | ||
package tripperware_test | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/gol4ng/httpware/v2" | ||
"github.com/gol4ng/httpware/v2/mocks" | ||
"github.com/gol4ng/httpware/v2/tripperware" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestEnable(t *testing.T) { | ||
roundTripperMock := &mocks.RoundTripper{} | ||
req := httptest.NewRequest(http.MethodGet, "http://fake-addr", nil) | ||
resp := &http.Response{ | ||
Status: "OK", | ||
StatusCode: http.StatusOK, | ||
ContentLength: 30, | ||
} | ||
roundTripperMock.On("RoundTrip", req).Return(resp, nil) | ||
|
||
tests := []struct { | ||
enable bool | ||
expectedExecuted bool | ||
}{ | ||
{ | ||
enable: true, | ||
expectedExecuted: true, | ||
}, | ||
{ | ||
enable: false, | ||
expectedExecuted: false, | ||
}, | ||
} | ||
|
||
executed := false | ||
dummyTripperware := func(next http.RoundTripper) http.RoundTripper { | ||
return httpware.RoundTripFunc(func(request *http.Request) (*http.Response, error) { | ||
executed = true | ||
return next.RoundTrip(request) | ||
}) | ||
} | ||
|
||
for k, test := range tests { | ||
executed = false | ||
t.Run(fmt.Sprintf("test %d (%v)", k, test), func(t *testing.T) { | ||
resp2, err := tripperware.Enable(test.enable, dummyTripperware, )(roundTripperMock).RoundTrip(req) | ||
|
||
assert.Nil(t, err) | ||
assert.Equal(t, resp, resp2) | ||
assert.Equal(t, test.expectedExecuted, executed) | ||
}) | ||
} | ||
} | ||
|
||
// ===================================================================================================================== | ||
// ========================================= EXAMPLES ================================================================== | ||
// ===================================================================================================================== | ||
|
||
func ExampleEnable() { | ||
port := ":9003" | ||
|
||
enableDummyTripperware := true //false | ||
dummyTripperware := func(next http.RoundTripper) http.RoundTripper { | ||
return httpware.RoundTripFunc(func(request *http.Request) (*http.Response, error) { | ||
request.Header.Set("FakeHeader", "this header is set when not /home url") | ||
return next.RoundTrip(request) | ||
}) | ||
} | ||
|
||
// create http client using the tripperwareStack as RoundTripper | ||
client := http.Client{ | ||
Transport: tripperware.Enable(enableDummyTripperware, dummyTripperware), | ||
} | ||
|
||
// create a server in order to show it work | ||
srv := http.NewServeMux() | ||
srv.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { | ||
fmt.Println("server receive request with request:", request.Header.Get("FakeHeader")) | ||
}) | ||
|
||
go func() { | ||
if err := http.ListenAndServe(port, srv); err != nil { | ||
panic(err) | ||
} | ||
}() | ||
|
||
_, _ = client.Get("http://localhost" + port + "/") | ||
|
||
// Output: | ||
//server receive request with request: this header is set when not /home url | ||
} |
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