-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve resolver interface and documentation
**What** - Update the BaseURLResolver interface so that it returns `url.Url` instead of `string` - Revert changes to the `FaaSConfig` so that the implementer can decide to override the proxy behavior - Add additional unit tests to verify the behavior of the NewHandlerFunc method - Add tests to verify behavior of non-allowed HTTP methods - Add docs explaining the configuration of the http client - Add docs to explain the proxy package and make them godoc friendly Signed-off-by: Lucas Roesler <roesler.lucas@gmail.com>
- Loading branch information
1 parent
eaa893b
commit cd31b38
Showing
5 changed files
with
145 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package proxy | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
"time" | ||
) | ||
|
||
type testBaseURLResolver struct { | ||
testServerBase string | ||
} | ||
|
||
func (tr *testBaseURLResolver) Resolve(name string) (url.URL, error) { | ||
return url.URL{ | ||
Scheme: "http", | ||
Host: tr.testServerBase, | ||
}, nil | ||
} | ||
func Test_NewHandlerFunc_Panic(t *testing.T) { | ||
defer func() { | ||
if r := recover(); r == nil { | ||
t.Errorf("should panic if resolver is nil") | ||
} | ||
}() | ||
|
||
NewHandlerFunc(time.Second, nil) | ||
} | ||
|
||
func Test_NewHandlerFunc_NoPanic(t *testing.T) { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
t.Errorf("should not panic if resolver is not nil") | ||
} | ||
}() | ||
|
||
proxyFunc := NewHandlerFunc(time.Second, &testBaseURLResolver{}) | ||
if proxyFunc == nil { | ||
t.Errorf("proxy handler func is nil") | ||
} | ||
} | ||
|
||
func Test_ProxyHandler_NonAllowedMethods(t *testing.T) { | ||
|
||
proxyFunc := NewHandlerFunc(time.Second, &testBaseURLResolver{}) | ||
|
||
nonAllowedMethods := []string{ | ||
http.MethodHead, http.MethodConnect, http.MethodOptions, http.MethodTrace, | ||
} | ||
|
||
for _, method := range nonAllowedMethods { | ||
t.Run(method+" method is not allowed", func(t *testing.T) { | ||
w := httptest.NewRecorder() | ||
req := httptest.NewRequest(method, "http://example.com/foo", nil) | ||
proxyFunc(w, req) | ||
resp := w.Result() | ||
if resp.StatusCode != http.StatusMethodNotAllowed { | ||
t.Errorf("expected status code `%d`, got `%d`", http.StatusMethodNotAllowed, resp.StatusCode) | ||
} | ||
}) | ||
} | ||
} |
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