From 44f9bfa484dc2abde9aa0c539a67f27806a17c9d Mon Sep 17 00:00:00 2001 From: Marcel Ludwig Date: Mon, 26 Oct 2020 10:11:57 +0100 Subject: [PATCH] Add path params test #59 --- server/mux_test.go | 103 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 server/mux_test.go diff --git a/server/mux_test.go b/server/mux_test.go new file mode 100644 index 000000000..8f1016d1c --- /dev/null +++ b/server/mux_test.go @@ -0,0 +1,103 @@ +package server_test + +import ( + "net/http" + "net/http/httptest" + "reflect" + "testing" + + "github.com/avenga/couper/config/request" + "github.com/avenga/couper/config/runtime" + "github.com/avenga/couper/errors" + "github.com/avenga/couper/server" +) + +func TestMux_FindHandler_PathParamContext(t *testing.T) { + + type noContentHandler http.Handler + var noContent noContentHandler = http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + rw.WriteHeader(http.StatusNoContent) + }) + + testOptions := &runtime.MuxOptions{ + APIErrTpl: errors.DefaultJSON, + FileErrTpl: errors.DefaultHTML, + EndpointRoutes: map[string]http.Handler{ + "/without": http.NotFoundHandler(), + "/with/{my}/parameter": noContent, + "/{section}/cms": noContent, + "/a/{b}": noContent, + "/{b}/a": noContent, + "/{section}/{project}": noContent, + "/project/{project}/**": noContent, + }, + } + + newReq := func(path string) *http.Request { + return httptest.NewRequest(http.MethodGet, path, nil) + } + + tests := []struct { + name string + req *http.Request + want http.Handler + expParams request.PathParameter + expWildcard string + }{ + {" /wo path params", newReq("/without"), http.NotFoundHandler(), request.PathParameter{}, ""}, + {" /w path params", newReq("/with/my123/parameter"), noContent, request.PathParameter{ + "my": "my123", + }, ""}, + {" /w 1st path param #1", newReq("/with/cms"), noContent, request.PathParameter{ + "section": "with", + }, ""}, + {" /w 1st path param #2", newReq("/c/a"), noContent, request.PathParameter{ + "b": "c", + }, ""}, + {" /w 2nd path param", newReq("/a/c"), noContent, request.PathParameter{ + "b": "c", + }, ""}, + {" /w two path param", newReq("/foo/bar"), noContent, request.PathParameter{ + "section": "foo", + "project": "bar", + }, ""}, + {" /w path param and expWildcard", newReq("/project/foo/bar/ha"), noContent, request.PathParameter{ + "project": "foo", + }, "bar/ha"}, + {" /w non existing path", newReq("/foo/{bar}/123"), errors.DefaultJSON.ServeError(errors.APIRouteNotFound), nil, ""}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mux := server.NewMux(testOptions) + + if got := mux.FindHandler(tt.req); reflect.DeepEqual(got, tt.want) { + t.Errorf("FindHandler() = %v, want %v", got, tt.want) + } + + paramCtx, ok := tt.req.Context().Value(request.PathParams).(request.PathParameter) + if !ok { + if tt.expParams == nil { + return + } + t.Errorf("Expected path parameters") + return + } + + if !reflect.DeepEqual(paramCtx, tt.expParams) { + t.Errorf("Path parameter context: %#v, want: %#v", paramCtx, tt.expParams) + } + + wildcardCtx, ok := tt.req.Context().Value(request.Wildcard).(string) + if !ok { + if tt.expWildcard == "" { + return + } + t.Fatal("Expected expWildcard value") + } + + if wildcardCtx != tt.expWildcard { + t.Errorf("Wildcard context: %q, want: %q", wildcardCtx, tt.expWildcard) + } + }) + } +}