/
web_test.go
65 lines (49 loc) · 1.22 KB
/
web_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
package main
import (
_ "bytes"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
func TestSayHello(test *testing.T) {
router := gin.New()
router.GET("/", SayHello)
request, _ := http.NewRequest("GET", "/", nil)
writer := httptest.NewRecorder()
router.ServeHTTP(writer, request)
assert.Equal(test, "Hello World", writer.Body.String())
}
func TestSendSomeJson(test *testing.T) {
router := gin.New()
router.GET("/somejson", SomeJSON)
type Msg struct {
Key string `json:"key"`
}
var expected Msg
expected.Key = "value"
//expectedJson, _ := json.Marshal(expected)
request, _ := http.NewRequest("GET", "/somejson", nil)
writer := httptest.NewRecorder()
router.ServeHTTP(writer, request)
actualBytes, _ := ioutil.ReadAll(writer.Body)
actualMsg := Msg{}
error := json.Unmarshal(actualBytes, &actualMsg)
if error != nil {
fmt.Println(error)
}
//Some debugging lines
//fmt.Println(string(expectedJson))
//fmt.Println(expectedJson)
//fmt.Println(actualBytes)
//fmt.Println(actualMsg)
assert.Equal(test, expected, actualMsg)
}
func TestWhatever(test *testing.T) {
x := whatever()
assert.Equal(test, "bob", x)
}