-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhandler_test.go
103 lines (99 loc) · 2.42 KB
/
handler_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
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
package omikuji
import (
"context"
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"
interactor "omikuji-app/pkg/api/app/interactor/omikuji"
mockInteractor "omikuji-app/pkg/api/app/interactor/omikuji/mock"
"omikuji-app/pkg/api/ocontext"
)
func TestNew(t *testing.T) {
type args struct {
i interactor.OmikujiInteractor
}
tests := []struct {
name string
args args
want http.Handler
}{
{
name: "omikujiHandlerの生成",
args: args{
i: mockInteractor.New(),
},
want: &omikujiHandler{omikujiInteractor: mockInteractor.New()},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := New(tt.args.i); !reflect.DeepEqual(got, tt.want) {
t.Errorf("New() = %v, want %v", got, tt.want)
}
})
}
}
func Test_omikujiHandler_ServeHTTP(t *testing.T) {
type fields struct {
omikujiInteractor interactor.OmikujiInteractor
}
type args struct {
w http.ResponseWriter
r *http.Request
}
tests := []struct {
name string
fields fields
args args
want string
wantCode int
}{
{
name: "リクエスト成功",
fields: fields{
omikujiInteractor: mockInteractor.New(),
},
args: args{
w: httptest.NewRecorder(),
r: httptest.NewRequest("GET", "/", nil).WithContext(ocontext.SetAccessTime(context.Background(), time.Now())),
},
want: "{\"id\":4,\"ruck\":\"吉\",\"message\":\"吉です!良い運勢ですね!\"}\n",
wantCode: http.StatusOK,
},
{
name: "リクエスト失敗",
fields: fields{
omikujiInteractor: mockInteractor.NewError(),
},
args: args{
w: httptest.NewRecorder(),
r: httptest.NewRequest("GET", "/", nil).WithContext(ocontext.SetAccessTime(context.Background(), time.Now())),
},
want: "抽選に失敗しました.\n",
wantCode: http.StatusInternalServerError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h := &omikujiHandler{
omikujiInteractor: tt.fields.omikujiInteractor,
}
h.ServeHTTP(tt.args.w, tt.args.r)
rw := tt.args.w.(*httptest.ResponseRecorder).Result()
defer rw.Body.Close()
if rw.StatusCode != tt.wantCode {
t.Errorf("unexpected status code: %v, want %v", rw.StatusCode, tt.wantCode)
}
b, err := ioutil.ReadAll(rw.Body)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if s := string(b); s != tt.want {
t.Errorf("unexpected response: %v, want %v", s, tt.want)
}
})
}
}