Skip to content

Instantly share code, notes, and snippets.

@piotrkubisa
Last active January 23, 2018 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piotrkubisa/7e3228d28adf8bac49be627e908d6b16 to your computer and use it in GitHub Desktop.
Save piotrkubisa/7e3228d28adf8bac49be627e908d6b16 to your computer and use it in GitHub Desktop.
Simple test for checking if `omitBasePath` implementation worked as expected. My PR submission for apex/gateway.
package gateway
import "testing"
// omitBasePath strips out the base path from the given path.
//
// It allows to support both API endpoints (default, auto-generated
// "execute-api" address and configured Base Path Mapping
// with a Custom Domain Name), while preserving the same routing
// registered on the http.Handler.
func omitBasePath(path string, basePath string) string {
if path == "/" || basePath == "" {
return path
}
if strings.HasPrefix(path, "/"+basePath) {
path = strings.Replace(path, basePath, "", 1)
}
if strings.HasPrefix(path, "//") {
path = path[1:]
}
return path
}
func Test_omitBasePath(t *testing.T) {
basePath := "items"
type args struct {
path string
basePath string
}
tests := []struct {
name string
args args
want string
}{
{"ListItems_WithBasePath_Default", args{"/", basePath}, "/"},
{"ListItems_WithBasePath_Custom", args{"/items", basePath}, "/"},
{"GetItem_WithBasePath_Default", args{"/123", basePath}, "/123"},
{"GetItem_WithBasePath_Custom", args{"/items/123", basePath}, "/123"},
{"ListItems_WithoutBasePath", args{"/", ""}, "/"},
{"GetItem_WithoutBasePath", args{"/123", ""}, "/123"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := omitBasePath(tt.args.path, tt.args.basePath); got != tt.want {
t.Errorf("omitBasePath() = %v, want %v", got, tt.want)
}
})
}
}
@piotrkubisa
Copy link
Author

go test -v -timeout 30s github.com\piotrkubisa\gateway -run ^Test_omitBasePath$

=== RUN   Test_omitBasePath
=== RUN   Test_omitBasePath/ListItems_WithBasePath_Default
=== RUN   Test_omitBasePath/ListItems_WithBasePath_Custom
=== RUN   Test_omitBasePath/GetItem_WithBasePath_Default
=== RUN   Test_omitBasePath/GetItem_WithBasePath_Custom
=== RUN   Test_omitBasePath/ListItems_WithoutBasePath
=== RUN   Test_omitBasePath/GetItem_WithoutBasePath
--- PASS: Test_omitBasePath (0.00s)
    --- PASS: Test_omitBasePath/ListItems_WithBasePath_Default (0.00s)
    --- PASS: Test_omitBasePath/ListItems_WithBasePath_Custom (0.00s)
    --- PASS: Test_omitBasePath/GetItem_WithBasePath_Default (0.00s)
    --- PASS: Test_omitBasePath/GetItem_WithBasePath_Custom (0.00s)
    --- PASS: Test_omitBasePath/ListItems_WithoutBasePath (0.00s)
    --- PASS: Test_omitBasePath/GetItem_WithoutBasePath (0.00s)
PASS
ok   github.com/piotrkubisa/gateway  0.050s
Success: Tests passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment