-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_request_function_handlers_test.go
116 lines (109 loc) · 3.2 KB
/
http_request_function_handlers_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
package main
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func mockReturnRequestStruct(url string, method httpMethod) RequestStruct {
bodyString := `{"foo": "bar"}`
headerString := `{"Content-type": "application/json; charset=UTF-8"}`
byteBody := bytes.NewBuffer([]byte(bodyString))
byteHeaders := []byte(headerString)
newRequestStruct := RequestStruct {
url: url,
chosenMethod: method,
byteRequestBody: byteBody,
byteRequestHeader: byteHeaders,
}
return newRequestStruct
}
func TestHandleGetMethod(t *testing.T) {
expectedResponse := `{"data": "dummy"}`
newServer := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, expectedResponse)
},
),
)
defer newServer.Close()
newMockedRequestStruct := mockReturnRequestStruct(newServer.URL, GET)
response, _ := handleRequest(newMockedRequestStruct)
if response.rawResponse != expectedResponse {
t.Errorf("Expected response to be %s got %s", expectedResponse, response.rawResponse)
}
}
func TestHandlePostMethod(t *testing.T) {
expectedResponse := `{"success": "id 101 created"}`
newServer := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("Expected a POST request, got %s", r.Method)
return
}
fmt.Fprintf(w, expectedResponse)
},
),
)
defer newServer.Close()
newMockedRequestStruct := mockReturnRequestStruct(newServer.URL, POST)
response, err := handleRequest(newMockedRequestStruct)
if err != nil {
t.Errorf("POST request failed: %s", err)
return
}
if response.rawResponse != expectedResponse {
t.Errorf("Expected response to be %s got %s", expectedResponse, response.rawResponse)
}
}
func TestHandlePutMethod(t *testing.T) {
expectedResponse := `{"success": "101 updated successfully"}`
newServer := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPut {
t.Errorf("Expected a PUT request, got %s", r.Method)
return
}
fmt.Fprintf(w, expectedResponse)
},
),
)
defer newServer.Close()
newMockedRequestStruct := mockReturnRequestStruct(newServer.URL, PUT)
response, err := handleRequest(newMockedRequestStruct)
if err != nil {
t.Errorf("PUT request failed: %s", err)
return
}
if response.rawResponse != expectedResponse {
t.Errorf("Expected response to be %s got %s", expectedResponse, response.rawResponse)
}
}
func TestHandleDeleteMethod(t *testing.T) {
expectedResponse := `{"success": "101 deleted successfully"}`
newServer := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodDelete {
t.Errorf("Expected a DELETE request, got %s", r.Method)
return
}
fmt.Fprintf(w, expectedResponse)
},
),
)
defer newServer.Close()
newMockedRequestStruct := mockReturnRequestStruct(newServer.URL, DELETE)
response, err := handleRequest(newMockedRequestStruct)
if err != nil {
t.Errorf("Delete request failed: %s", err)
return
}
if response.rawResponse != expectedResponse {
t.Errorf("Expected response to be %s got %s", expectedResponse, response.rawResponse)
}
}