-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
51 lines (40 loc) · 1.26 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
package main
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
// TestGreet tests the Greet function to ensure it returns the correct greeting.
func TestGreet(t *testing.T) {
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(Greet)
handler.ServeHTTP(rr, req)
expected := "Hello World!"
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expected)
}
}
// TestMultiply tests the Multiply function to ensure it correctly multiplies two numbers and returns the result.
func TestMultiply(t *testing.T) {
input := &Input{A: 2, B: 3}
inputJSON, _ := json.Marshal(input)
req, err := http.NewRequest("POST", "/multiply", bytes.NewBuffer(inputJSON))
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(Multiply)
handler.ServeHTTP(rr, req)
var output Input
_ = json.Unmarshal(rr.Body.Bytes(), &output)
expected := float32(6)
if output.Answer != expected {
t.Errorf("handler returned unexpected body: got %v want %v", output.Answer, expected)
}
}