Last active
January 23, 2018 17:54
-
-
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.
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
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) | |
} | |
}) | |
} | |
} |
Author
piotrkubisa
commented
Jan 23, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment