From 19662192532ff7483fa62658d8a25e74fa71b328 Mon Sep 17 00:00:00 2001 From: Rafael Dantas Justo Date: Wed, 22 Aug 2018 02:55:11 +0100 Subject: [PATCH] Add default headers to configuration (#44) * Add default headers to configuration The dynamic values could be found in the HTTP headers (e.g. Etag). This new configuration entry will work on the same way of the current defaults, but being applied to the header instead of the JSON body. Resolves #43 * Move HTTP headers overrides to defaults section of the config --- README.md | 3 ++- assert.go | 16 ++++++++++++++-- example/__snapshots__/example.snapshot | 2 ++ example/abide.json | 1 + example/main.go | 3 +++ 5 files changed, 22 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 07753aa..d947cd5 100644 --- a/README.md +++ b/README.md @@ -81,13 +81,14 @@ In some cases, attributes in a JSON response can by dynamic (e.g unique id's, da ```json { "defaults": { + "Etag": "default-etag-value", "updated_at": 0, "foo": "foobar" } } ``` -When used with `AssertHTTPResponse`, for any response with `Content-Type: application/json`, the key-value pairs in `defaults` will be used to override the JSON response, allowing for consistent snapshot testing. +When used with `AssertHTTPResponse`, for any response with `Content-Type: application/json`, the key-value pairs in `defaults` will be used to override the JSON response, allowing for consistent snapshot testing. Any HTTP headers will also be override for key matches in `defaults`. ## Using custom `__snapshot__` directory diff --git a/assert.go b/assert.go index 355aace..aa18c6f 100644 --- a/assert.go +++ b/assert.go @@ -66,10 +66,22 @@ func assertHTTP(t *testing.T, id string, body []byte, isJSON bool) { } data := string(body) + lines := strings.Split(strings.TrimSpace(data), "\n") + + // empty line identifies the end of the HTTP header + for i, line := range lines { + if line == "" { + break + } + + headerItem := strings.Split(line, ":") + if def, ok := config.Defaults[headerItem[0]]; ok { + lines[i] = fmt.Sprintf("%s: %s", headerItem[0], def) + } + } // If the response body is JSON, indent. if isJSON { - lines := strings.Split(strings.TrimSpace(data), "\n") jsonStr := lines[len(lines)-1] var jsonIface map[string]interface{} @@ -90,9 +102,9 @@ func assertHTTP(t *testing.T, id string, body []byte, isJSON bool) { t.Fatal(err) } lines[len(lines)-1] = string(out) - data = strings.Join(lines, "\n") } + data = strings.Join(lines, "\n") createOrUpdateSnapshot(t, id, data) } diff --git a/example/__snapshots__/example.snapshot b/example/__snapshots__/example.snapshot index e9991f2..5a1b26f 100755 --- a/example/__snapshots__/example.snapshot +++ b/example/__snapshots__/example.snapshot @@ -2,6 +2,7 @@ HTTP/1.1 200 OK Connection: close Content-Type: application/json +Etag: default-etag-value { "foo": "foobar" @@ -38,6 +39,7 @@ Hello World. HTTP/1.1 200 OK Connection: close Content-Type: application/json +Etag: default-etag-value { "post": { diff --git a/example/abide.json b/example/abide.json index 4c7fa09..4b428ff 100644 --- a/example/abide.json +++ b/example/abide.json @@ -1,5 +1,6 @@ { "defaults": { + "Etag": "default-etag-value", "updated_at": 0, "foo": "foobar" } diff --git a/example/main.go b/example/main.go index c579635..c3f76eb 100644 --- a/example/main.go +++ b/example/main.go @@ -3,6 +3,7 @@ package main import ( "encoding/json" "net/http" + "strconv" "time" "github.com/beme/abide/example/models" @@ -20,6 +21,7 @@ func firstHandler(w http.ResponseWriter, r *http.Request) { } w.Header().Set("Content-Type", "application/json") + w.Header().Set("Etag", strconv.FormatInt(time.Now().UnixNano(), 10)) w.Write(body) } @@ -47,6 +49,7 @@ func secondHandler(w http.ResponseWriter, r *http.Request) { } w.Header().Set("Content-Type", "application/json") + w.Header().Set("Etag", strconv.FormatInt(time.Now().UnixNano(), 10)) w.Write(body) }