-
Notifications
You must be signed in to change notification settings - Fork 113
chore: refactor middleware setup in server #554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
beeme1mr
merged 16 commits into
open-feature:main
from
bacherfl:chore/refactor-middleware
Mar 24, 2023
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
c7b41f1
chore: refactor middleware setup
bacherfl 5e996be
added unit tests for injecting middleware
bacherfl 4ac22ae
remove obsolete code
bacherfl 5ca7f7a
added simple unit test to verify middleware
bacherfl a97c144
adapted to recent refactoring
bacherfl 9b9e023
adopted the test to avoid race conditions
bacherfl cee0db3
remove obsolete code
bacherfl 1a90b2b
Merge remote-tracking branch 'upstream/main' into chore/refactor-midd…
bacherfl 09d28ee
revert out of scope change
bacherfl ffda470
gofumpt'ed changed files
bacherfl 5870027
Merge remote-tracking branch 'upstream/main' into chore/refactor-midd…
bacherfl b54358b
fixed merge conflicts
bacherfl 9197e49
Merge branch 'main' into chore/refactor-middleware
bacherfl c16a676
Merge branch 'main' into chore/refactor-middleware
bacherfl 09bc299
incorporated PR review
bacherfl 7778c85
Merge branch 'main' into chore/refactor-middleware
Kavindu-Dodan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,46 @@ | ||
| package cors | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "github.com/rs/cors" | ||
| ) | ||
|
|
||
| type Middleware struct { | ||
| cors *cors.Cors | ||
| } | ||
|
|
||
| func New(allowedOrigins []string) *Middleware { | ||
| return &Middleware{ | ||
| cors: cors.New(cors.Options{ | ||
| AllowedMethods: []string{ | ||
| http.MethodHead, | ||
| http.MethodGet, | ||
| http.MethodPost, | ||
| http.MethodPut, | ||
| http.MethodPatch, | ||
| http.MethodDelete, | ||
| }, | ||
| AllowedOrigins: allowedOrigins, | ||
| AllowedHeaders: []string{"*"}, | ||
| ExposedHeaders: []string{ | ||
| // Content-Type is in the default safelist. | ||
| "Accept", | ||
| "Accept-Encoding", | ||
| "Accept-Post", | ||
| "Connect-Accept-Encoding", | ||
| "Connect-Content-Encoding", | ||
| "Content-Encoding", | ||
| "Grpc-Accept-Encoding", | ||
| "Grpc-Encoding", | ||
| "Grpc-Message", | ||
| "Grpc-Status", | ||
| "Grpc-Status-Details-Bin", | ||
| }, | ||
| }), | ||
| } | ||
| } | ||
|
|
||
| func (c Middleware) Handler(handler http.Handler) http.Handler { | ||
| return c.cors.Handler(handler) | ||
| } |
This file contains hidden or 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,44 @@ | ||
| package cors | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/golang/mock/gomock" | ||
| middlewaremock "github.com/open-feature/flagd/core/pkg/service/middleware/mock" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestMiddleware(t *testing.T) { | ||
| ctrl := gomock.NewController(t) | ||
| mockMw := middlewaremock.NewMockIMiddleware(ctrl) | ||
|
|
||
| handlerFunc := http.HandlerFunc( | ||
| func(writer http.ResponseWriter, request *http.Request) { | ||
| writer.WriteHeader(http.StatusOK) | ||
| }, | ||
| ) | ||
|
|
||
| mockMw.EXPECT().Handler(gomock.Any()).Return(handlerFunc) | ||
|
|
||
| ts := httptest.NewServer(handlerFunc) | ||
|
|
||
| defer ts.Close() | ||
|
|
||
| mw := New([]string{"*"}) | ||
| require.NotNil(t, mw) | ||
|
|
||
| // wrap the cors middleware around the mock to make sure the wrapped handler is called by the cors middleware | ||
| ts.Config.Handler = mw.Handler(mockMw.Handler(handlerFunc)) | ||
|
|
||
| req, err := http.NewRequest(http.MethodGet, ts.URL, nil) | ||
|
|
||
| require.Nil(t, err) | ||
|
|
||
| client := http.DefaultClient | ||
| resp, err := client.Do(req) | ||
|
|
||
| require.Nil(t, err) | ||
| require.Equal(t, http.StatusOK, resp.StatusCode) | ||
| } |
This file contains hidden or 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,18 @@ | ||
| package h2c | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "golang.org/x/net/http2" | ||
| "golang.org/x/net/http2/h2c" | ||
| ) | ||
|
|
||
| type Middleware struct{} | ||
|
|
||
| func New() *Middleware { | ||
| return &Middleware{} | ||
| } | ||
|
|
||
| func (m Middleware) Handler(handler http.Handler) http.Handler { | ||
| return h2c.NewHandler(handler, &http2.Server{}) | ||
| } |
This file contains hidden or 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,39 @@ | ||
| package h2c | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/golang/mock/gomock" | ||
| middlewaremock "github.com/open-feature/flagd/core/pkg/service/middleware/mock" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestMiddleware(t *testing.T) { | ||
| ctrl := gomock.NewController(t) | ||
| mockMw := middlewaremock.NewMockIMiddleware(ctrl) | ||
|
|
||
| handlerFunc := http.HandlerFunc( | ||
| func(writer http.ResponseWriter, request *http.Request) { | ||
| writer.WriteHeader(http.StatusOK) | ||
| }, | ||
| ) | ||
|
|
||
| mockMw.EXPECT().Handler(gomock.Any()).Return(handlerFunc) | ||
|
|
||
| ts := httptest.NewServer(handlerFunc) | ||
|
|
||
| defer ts.Close() | ||
|
|
||
| mw := New() | ||
| require.NotNil(t, mw) | ||
|
|
||
| // wrap the h2c middleware around the mock to make sure the wrapped handler is called by the h2c middleware | ||
| ts.Config.Handler = mw.Handler(mockMw.Handler(handlerFunc)) | ||
|
|
||
| resp, err := http.Get(ts.URL) | ||
|
|
||
| require.Nil(t, err) | ||
| require.Equal(t, http.StatusOK, resp.StatusCode) | ||
| } |
This file contains hidden or 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,9 @@ | ||
| package middleware | ||
|
|
||
| import ( | ||
| "net/http" | ||
| ) | ||
|
|
||
| type IMiddleware interface { | ||
| Handler(handler http.Handler) http.Handler | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.