-
Notifications
You must be signed in to change notification settings - Fork 1
/
responses_test.go
52 lines (43 loc) · 1.14 KB
/
responses_test.go
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
package gojison
import (
"encoding/json"
"errors"
"net/http/httptest"
"testing"
)
func TestError(t *testing.T) {
w := httptest.NewRecorder()
var result map[string]string
Error(w, errors.New("test error"), 0)
if w.Code != 400 {
t.Error("Expected response code to be 400 when 0 passed, but was %d.", w.Code)
}
json.NewDecoder(w.Body).Decode(&result)
got := result["error"]
if got != "test error" {
t.Errorf("Expected error to be \"test error\", but was %s!", got)
}
w = httptest.NewRecorder()
Error(w, errors.New("not found"), 404)
if w.Code != 404 {
t.Error("Expected response code to be 404 but was %d.", w.Code)
}
}
func TestSuccess(t *testing.T) {
w := httptest.NewRecorder()
var result map[string]string
Success(w, "okey", 0)
if w.Code != 200 {
t.Error("Expected response code to be 400 when 0 passed, but was %d.", w.Code)
}
json.NewDecoder(w.Body).Decode(&result)
got := result["success"]
if got != "okey" {
t.Errorf("Expected error to be \"okey\", but was %s!", got)
}
w = httptest.NewRecorder()
Success(w, "created", 201)
if w.Code != 201 {
t.Error("Expected response code to be 201 but was %d.", w.Code)
}
}