From 471de833ee2a3726a54e7791379dbbfb58ff69a0 Mon Sep 17 00:00:00 2001 From: Piotr Icikowski Date: Wed, 12 Jan 2022 22:41:09 +0100 Subject: [PATCH] Change default handler's response structure - Headers' values are now returned as single comma-separated string instead of list - Added query values logging --- application/service/builder_test.go | 4 ++-- application/service/handlers.go | 13 ++++++++++++- application/service/types.go | 9 +++++---- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/application/service/builder_test.go b/application/service/builder_test.go index 65adf3f..a27f7ed 100644 --- a/application/service/builder_test.go +++ b/application/service/builder_test.go @@ -50,13 +50,13 @@ func TestPrepareServer(t *testing.T) { method: http.MethodGet, path: "/", expectedStatus: http.StatusOK, - expectedKeys: &[]string{"host", "path", "method", "headers"}, + expectedKeys: &[]string{"host", "path", "method", "headers", "queries"}, }, "get default endpoint on other path": { method: http.MethodGet, path: "/a/b/c", expectedStatus: http.StatusOK, - expectedKeys: &[]string{"host", "path", "method", "headers"}, + expectedKeys: &[]string{"host", "path", "method", "headers", "queries"}, }, "get on multiple-methods route": { method: http.MethodGet, diff --git a/application/service/handlers.go b/application/service/handlers.go index 10745ba..bd37703 100644 --- a/application/service/handlers.go +++ b/application/service/handlers.go @@ -121,11 +121,22 @@ func getDefaultHandler(log zerolog.Logger) http.Handler { ). Logger() + headers := map[string]string{} + for key, value := range r.Header { + headers[key] = strings.Join(value, ",") + } + + queries := map[string]string{} + for key, value := range r.URL.Query() { + queries[key] = strings.Join(value, ",") + } + response := defaultResponse{ Host: r.Host, Path: r.URL.Path, Method: r.Method, - Headers: r.Header, + Headers: headers, + Queries: queries, } mediaType := r.Header.Get("Accept") diff --git a/application/service/types.go b/application/service/types.go index 17c6497..58c9d82 100644 --- a/application/service/types.go +++ b/application/service/types.go @@ -1,8 +1,9 @@ package service type defaultResponse struct { - Host string `json:"host" yaml:"host"` - Path string `json:"path" yaml:"path"` - Method string `json:"method" yaml:"method"` - Headers map[string][]string `json:"headers" yaml:"headers"` + Host string `json:"host" yaml:"host"` + Path string `json:"path" yaml:"path"` + Method string `json:"method" yaml:"method"` + Headers map[string]string `json:"headers" yaml:"headers"` + Queries map[string]string `json:"queries" yaml:"queries"` }