| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/google/jsonapi" |
| 10 | ) |
| 11 | |
| 12 | func TestExampleHandler_post(t *testing.T) { |
| 13 | blog := fixtureBlogCreate(1) |
| 14 | requestBody := bytes.NewBuffer(nil) |
| 15 | jsonapi.MarshalOnePayloadEmbedded(requestBody, blog) |
| 16 | |
| 17 | r, err := http.NewRequest(http.MethodPost, "/blogs?id=1", requestBody) |
| 18 | if err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | r.Header.Set(headerAccept, jsonapi.MediaType) |
| 22 | |
| 23 | rr := httptest.NewRecorder() |
| 24 | handler := &ExampleHandler{} |
| 25 | handler.ServeHTTP(rr, r) |
| 26 | |
| 27 | if e, a := http.StatusCreated, rr.Code; e != a { |
| 28 | t.Fatalf("Expected a status of %d, got %d", e, a) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func TestExampleHandler_put(t *testing.T) { |
| 33 | blogs := []interface{}{ |
| 34 | fixtureBlogCreate(1), |
| 35 | fixtureBlogCreate(2), |
| 36 | fixtureBlogCreate(3), |
| 37 | } |
| 38 | requestBody := bytes.NewBuffer(nil) |
| 39 | jsonapi.MarshalPayload(requestBody, blogs) |
| 40 | |
| 41 | r, err := http.NewRequest(http.MethodPut, "/blogs", requestBody) |
| 42 | if err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | r.Header.Set(headerAccept, jsonapi.MediaType) |
| 46 | |
| 47 | rr := httptest.NewRecorder() |
| 48 | handler := &ExampleHandler{} |
| 49 | handler.ServeHTTP(rr, r) |
| 50 | |
| 51 | if e, a := http.StatusOK, rr.Code; e != a { |
| 52 | t.Fatalf("Expected a status of %d, got %d", e, a) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestExampleHandler_get_show(t *testing.T) { |
| 57 | r, err := http.NewRequest(http.MethodGet, "/blogs?id=1", nil) |
| 58 | if err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | r.Header.Set(headerAccept, jsonapi.MediaType) |
| 62 | |
| 63 | rr := httptest.NewRecorder() |
| 64 | handler := &ExampleHandler{} |
| 65 | handler.ServeHTTP(rr, r) |
| 66 | |
| 67 | if e, a := http.StatusOK, rr.Code; e != a { |
| 68 | t.Fatalf("Expected a status of %d, got %d", e, a) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | func TestExampleHandler_get_list(t *testing.T) { |
| 73 | r, err := http.NewRequest(http.MethodGet, "/blogs", nil) |
| 74 | if err != nil { |
| 75 | t.Fatal(err) |
| 76 | } |
| 77 | r.Header.Set(headerAccept, jsonapi.MediaType) |
| 78 | |
| 79 | rr := httptest.NewRecorder() |
| 80 | handler := &ExampleHandler{} |
| 81 | handler.ServeHTTP(rr, r) |
| 82 | |
| 83 | if e, a := http.StatusOK, rr.Code; e != a { |
| 84 | t.Fatalf("Expected a status of %d, got %d", e, a) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func TestHttpErrorWhenHeaderDoesNotMatch(t *testing.T) { |
| 89 | r, err := http.NewRequest(http.MethodGet, "/blogs", nil) |
| 90 | if err != nil { |
| 91 | t.Fatal(err) |
| 92 | } |
| 93 | r.Header.Set(headerAccept, "application/xml") |
| 94 | |
| 95 | rr := httptest.NewRecorder() |
| 96 | handler := &ExampleHandler{} |
| 97 | handler.ServeHTTP(rr, r) |
| 98 | |
| 99 | if rr.Code != http.StatusUnsupportedMediaType { |
| 100 | t.Fatal("expected Unsupported Media Type staus error") |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | func TestHttpErrorWhenMethodDoesNotMatch(t *testing.T) { |
| 105 | r, err := http.NewRequest(http.MethodPatch, "/blogs", nil) |
| 106 | if err != nil { |
| 107 | t.Fatal(err) |
| 108 | } |
| 109 | r.Header.Set(headerAccept, jsonapi.MediaType) |
| 110 | |
| 111 | rr := httptest.NewRecorder() |
| 112 | handler := &ExampleHandler{} |
| 113 | handler.ServeHTTP(rr, r) |
| 114 | |
| 115 | if rr.Code != http.StatusNotFound { |
| 116 | t.Fatal("expected HTTP Status Not Found status error") |
| 117 | } |
| 118 | } |