-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_test.go
135 lines (127 loc) · 4.63 KB
/
my_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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
func TestAdd(t *testing.T) {
// Create a test HTTP request with the necessary parameters.
form := url.Values{}
form.Add("num1", "2")
form.Add("num2", "3")
req, err := http.NewRequest("POST", "/add", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if err != nil {
t.Fatal(err)
}
// Create a test ResponseWriter to capture the response.
recorder := httptest.NewRecorder()
// Call the AddNumbers function passing the test Request and ResponseWriter.
AddNumbers(recorder, req)
// Check the expected status code.
if recorder.Code != http.StatusOK {
t.Errorf("Expected status code %d but got %d", http.StatusOK, recorder.Code)
}
// Check the body of the expected JSON response.
expectedResponse := `{"result":5}`
if recorder.Body.String() != expectedResponse {
t.Errorf("Expected response %s but got %s", expectedResponse, recorder.Body.String())
}
}
func TestBin(t *testing.T) {
// Create a test HTTP request with the necessary parameters.
form := url.Values{}
form.Add("num1", "2023")
req, err := http.NewRequest("POST", "/bin", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if err != nil {
t.Fatal(err)
}
// Create a test ResponseWriter to capture the response.
recorder := httptest.NewRecorder()
// Call the AddNumbers function passing the test Request and ResponseWriter.
ConvertIntToBinary(recorder, req)
// Check the expected status code.
if recorder.Code != http.StatusOK {
t.Errorf("Expected status code %d but got %d", http.StatusOK, recorder.Code)
}
// Check the body of the expected JSON response.
expectedResponse := `{"Message":"11111100111"}`
if recorder.Body.String() != expectedResponse {
t.Errorf("Expected response %s but got %s", expectedResponse, recorder.Body.String())
}
}
func TestDiv(t *testing.T) {
// Create a test HTTP request with the necessary parameters.
form := url.Values{}
form.Add("num1", "6")
form.Add("num2", "2")
req, err := http.NewRequest("POST", "/div", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if err != nil {
t.Fatal(err)
}
// Create a test ResponseWriter to capture the response.
recorder := httptest.NewRecorder()
// Call the AddNumbers function passing the test Request and ResponseWriter.
DivisionNumbers(recorder, req)
// Check the expected status code.
if recorder.Code != http.StatusOK {
t.Errorf("Expected status code %d but got %d", http.StatusOK, recorder.Code)
}
// Check the body of the expected JSON response.
expectedResponse := `{"result":3}`
if recorder.Body.String() != expectedResponse {
t.Errorf("Expected response %s but got %s", expectedResponse, recorder.Body.String())
}
}
func TestMul(t *testing.T) {
// Create a test HTTP request with the necessary parameters.
form := url.Values{}
form.Add("num1", "6")
form.Add("num2", "2")
req, err := http.NewRequest("POST", "/mul", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if err != nil {
t.Fatal(err)
}
// Create a test ResponseWriter to capture the response.
recorder := httptest.NewRecorder()
// Call the AddNumbers function passing the test Request and ResponseWriter.
MultiplyNumbers(recorder, req)
// Check the expected status code.
if recorder.Code != http.StatusOK {
t.Errorf("Expected status code %d but got %d", http.StatusOK, recorder.Code)
}
// Check the body of the expected JSON response.
expectedResponse := `{"result":12}`
if recorder.Body.String() != expectedResponse {
t.Errorf("Expected response %s but got %s", expectedResponse, recorder.Body.String())
}
}
func TestSub(t *testing.T) {
// Create a test HTTP request with the necessary parameters.
form := url.Values{}
form.Add("num1", "6")
form.Add("num2", "2")
req, err := http.NewRequest("POST", "/sub", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if err != nil {
t.Fatal(err)
}
// Create a test ResponseWriter to capture the response.
recorder := httptest.NewRecorder()
// Call the AddNumbers function passing the test Request and ResponseWriter.
SubtractionNumbers(recorder, req)
// Check the expected status code.
if recorder.Code != http.StatusOK {
t.Errorf("Expected status code %d but got %d", http.StatusOK, recorder.Code)
}
// Check the body of the expected JSON response.
expectedResponse := `{"result":4}`
if recorder.Body.String() != expectedResponse {
t.Errorf("Expected response %s but got %s", expectedResponse, recorder.Body.String())
}
}