-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
118 lines (90 loc) · 2.93 KB
/
main_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"testing"
"net/http"
"net/http/httptest"
"log"
"os"
"encoding/json"
)
func TestHandler(t *testing.T){
req,err := http.NewRequest("GET","http://localhost:8080/",nil)
if err != nil{
t.Fatalf("could not create request: %v",err)
}
rec := httptest.NewRecorder()
l := log.New(os.Stdout,"orders-api",log.LstdFlags)
handler := NewHandler(l)
handler.ServeHTTP(rec,req)
result :=rec.Result()
if result.StatusCode != http.StatusOK{
t.Errorf("excepted status OK; got %v and body: ",result.Status)
}
var prod []Product
if err := json.NewDecoder(result.Body).Decode(&prod); err != nil{
t.Fatalf("decoder err %v",err)
}
}
func TestHandlerGetOrderById(t *testing.T){
req,err := http.NewRequest("GET","http://localhost:8080/1",nil)
if err != nil{
t.Fatalf("could not create request: %v",err)
}
rec := httptest.NewRecorder()
l := log.New(os.Stdout,"orders-api",log.LstdFlags)
handler := NewHandler(l)
handler.ServeHTTP(rec,req)
result :=rec.Result()
if result.StatusCode != http.StatusOK{
t.Errorf("excepted status OK; got %v and body: ",result.Status)
}
var prod []Product
if err := json.NewDecoder(result.Body).Decode(&prod); err != nil{
t.Fatalf("decoder err %v",err)
}
}
func TestHandlerPost(t *testing.T){
req,err := http.NewRequest("POST","http://localhost:8080/",nil)
if err != nil{
t.Fatalf("could not create request: %v",err)
}
rec := httptest.NewRecorder()
l := log.New(os.Stdout,"orders-api",log.LstdFlags)
handler := NewHandler(l)
handler.ServeHTTP(rec,req)
result :=rec.Result()
if result.StatusCode != http.StatusBadRequest{
// response status should be bad request because the api doesn't handle put method
t.Fatalf("Request should be bad request, but got: %v,%v",result.Status,err)
}
}
func TestHandlerDelete(t *testing.T){
req,err := http.NewRequest("DELETE","http://localhost:8080/",nil)
if err != nil{
t.Fatalf("could not create request: %v",err)
}
rec := httptest.NewRecorder()
l := log.New(os.Stdout,"orders-api",log.LstdFlags)
handler := NewHandler(l)
handler.ServeHTTP(rec,req)
result :=rec.Result()
if result.StatusCode != http.StatusBadRequest{
// response status should be bad request because we chose method delete, but didn't chose any id
t.Fatalf("Request should be bad request, but got: %v,%v",result.Status,err)
}
}
func TestHandlerPut(t *testing.T){
req,err := http.NewRequest("PUT","http://localhost:8080/1?id=2",nil)
if err != nil{
t.Fatalf("could not create request: %v",err)
}
rec := httptest.NewRecorder()
l := log.New(os.Stdout,"orders-api",log.LstdFlags)
handler := NewHandler(l)
handler.ServeHTTP(rec,req)
result :=rec.Result()
if result.StatusCode != http.StatusOK{
// response status should be bad request because we chose method delete, but didn't chose any id
t.Fatalf("Request should be bad request, but got: %v,%v",result.Status,err)
}
}