File: handler_test.go

package info (click to toggle)
golang-github-svanharmelen-jsonapi 1.0.0%2Bgit20180618.0c0828c-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 232 kB
  • sloc: makefile: 5
file content (118 lines) | stat: -rw-r--r-- 2,739 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main

import (
	"bytes"
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/google/jsonapi"
)

func TestExampleHandler_post(t *testing.T) {
	blog := fixtureBlogCreate(1)
	requestBody := bytes.NewBuffer(nil)
	jsonapi.MarshalOnePayloadEmbedded(requestBody, blog)

	r, err := http.NewRequest(http.MethodPost, "/blogs?id=1", requestBody)
	if err != nil {
		t.Fatal(err)
	}
	r.Header.Set(headerAccept, jsonapi.MediaType)

	rr := httptest.NewRecorder()
	handler := &ExampleHandler{}
	handler.ServeHTTP(rr, r)

	if e, a := http.StatusCreated, rr.Code; e != a {
		t.Fatalf("Expected a status of %d, got %d", e, a)
	}
}

func TestExampleHandler_put(t *testing.T) {
	blogs := []interface{}{
		fixtureBlogCreate(1),
		fixtureBlogCreate(2),
		fixtureBlogCreate(3),
	}
	requestBody := bytes.NewBuffer(nil)
	jsonapi.MarshalPayload(requestBody, blogs)

	r, err := http.NewRequest(http.MethodPut, "/blogs", requestBody)
	if err != nil {
		t.Fatal(err)
	}
	r.Header.Set(headerAccept, jsonapi.MediaType)

	rr := httptest.NewRecorder()
	handler := &ExampleHandler{}
	handler.ServeHTTP(rr, r)

	if e, a := http.StatusOK, rr.Code; e != a {
		t.Fatalf("Expected a status of %d, got %d", e, a)
	}
}

func TestExampleHandler_get_show(t *testing.T) {
	r, err := http.NewRequest(http.MethodGet, "/blogs?id=1", nil)
	if err != nil {
		t.Fatal(err)
	}
	r.Header.Set(headerAccept, jsonapi.MediaType)

	rr := httptest.NewRecorder()
	handler := &ExampleHandler{}
	handler.ServeHTTP(rr, r)

	if e, a := http.StatusOK, rr.Code; e != a {
		t.Fatalf("Expected a status of %d, got %d", e, a)
	}
}

func TestExampleHandler_get_list(t *testing.T) {
	r, err := http.NewRequest(http.MethodGet, "/blogs", nil)
	if err != nil {
		t.Fatal(err)
	}
	r.Header.Set(headerAccept, jsonapi.MediaType)

	rr := httptest.NewRecorder()
	handler := &ExampleHandler{}
	handler.ServeHTTP(rr, r)

	if e, a := http.StatusOK, rr.Code; e != a {
		t.Fatalf("Expected a status of %d, got %d", e, a)
	}
}

func TestHttpErrorWhenHeaderDoesNotMatch(t *testing.T) {
	r, err := http.NewRequest(http.MethodGet, "/blogs", nil)
	if err != nil {
		t.Fatal(err)
	}
	r.Header.Set(headerAccept, "application/xml")

	rr := httptest.NewRecorder()
	handler := &ExampleHandler{}
	handler.ServeHTTP(rr, r)

	if rr.Code != http.StatusUnsupportedMediaType {
		t.Fatal("expected Unsupported Media Type staus error")
	}
}

func TestHttpErrorWhenMethodDoesNotMatch(t *testing.T) {
	r, err := http.NewRequest(http.MethodPatch, "/blogs", nil)
	if err != nil {
		t.Fatal(err)
	}
	r.Header.Set(headerAccept, jsonapi.MediaType)

	rr := httptest.NewRecorder()
	handler := &ExampleHandler{}
	handler.ServeHTTP(rr, r)

	if rr.Code != http.StatusNotFound {
		t.Fatal("expected HTTP Status Not Found status error")
	}
}