Skip to content

Commit

Permalink
Add default headers to configuration (#44)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rafaeljusto authored and sjkaliski committed Aug 22, 2018
1 parent 1970c2b commit 1966219
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 14 additions & 2 deletions assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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)
}

Expand Down
2 changes: 2 additions & 0 deletions example/__snapshots__/example.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
HTTP/1.1 200 OK
Connection: close
Content-Type: application/json
Etag: default-etag-value

{
"foo": "foobar"
Expand Down Expand Up @@ -38,6 +39,7 @@ Hello World.
HTTP/1.1 200 OK
Connection: close
Content-Type: application/json
Etag: default-etag-value

{
"post": {
Expand Down
1 change: 1 addition & 0 deletions example/abide.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"defaults": {
"Etag": "default-etag-value",
"updated_at": 0,
"foo": "foobar"
}
Expand Down
3 changes: 3 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"net/http"
"strconv"
"time"

"github.com/beme/abide/example/models"
Expand All @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 1966219

Please sign in to comment.