-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
101 lines (96 loc) · 3.77 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
package main
import (
"api-go-gin/controllers"
"api-go-gin/initializers"
"api-go-gin/tests"
"bytes"
"io/ioutil"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func TestEndpointCreateFoobar(t *testing.T) {
initializers.StartDatabaseConnect()
r := tests.RouteSetupTesting()
r.POST("/foobar", controllers.CreateFoobar)
contents := []byte(tests.MyFoobarContents)
request, _ := http.NewRequest("POST", "/foobar", bytes.NewBuffer(contents))
response := httptest.NewRecorder()
r.ServeHTTP(response, request)
tests.SharedTestID = tests.GetIdFromHttpResponse(response)
assert.Equal(t, http.StatusOK, response.Code, "Not expected status code")
}
func TestEndpointFoobarAll(t *testing.T) {
initializers.StartDatabaseConnect()
r := tests.RouteSetupTesting()
r.GET("/foobar", controllers.ShownFoobar)
request, _ := http.NewRequest("GET", "/foobar", nil)
response := httptest.NewRecorder()
r.ServeHTTP(response, request)
assert.Equal(t, http.StatusOK, response.Code, "Not expected status code")
}
func TestEndpointFoobarByParamId(t *testing.T) {
initializers.StartDatabaseConnect()
r := tests.RouteSetupTesting()
r.GET("/foobar/:id", controllers.ShownFoobarByParamId)
endpointPath := "/foobar/" + strconv.Itoa(tests.SharedTestID)
request, _ := http.NewRequest("GET", endpointPath, nil)
response := httptest.NewRecorder()
r.ServeHTTP(response, request)
assert.Equal(t, http.StatusOK, response.Code, "Not expected status code")
}
func TestEndpointFoobarByParamReg(t *testing.T) {
initializers.StartDatabaseConnect()
r := tests.RouteSetupTesting()
r.GET("/foobar/param/:reg", controllers.ShownFoobarByParamReg)
request, _ := http.NewRequest("GET", "/foobar/param/1010", nil)
response := httptest.NewRecorder()
r.ServeHTTP(response, request)
assert.Equal(t, http.StatusOK, response.Code, "Not expected status code")
}
func TestEndpointEditFoobarByParamId(t *testing.T) {
initializers.StartDatabaseConnect()
r := tests.RouteSetupTesting()
r.PATCH("/foobar/:id", controllers.EditFoobarByParamId)
contents := []byte(tests.MyFoobarEdited)
endpointPath := "/foobar/" + strconv.Itoa(tests.SharedTestID)
request, _ := http.NewRequest("PATCH", endpointPath, bytes.NewBuffer(contents))
response := httptest.NewRecorder()
r.ServeHTTP(response, request)
tests.SharedTestID = tests.GetIdFromHttpResponse(response)
assert.Equal(t, http.StatusOK, response.Code, "Not expected status code")
}
func TestEndpointDeleteFoobarByParamId(t *testing.T) {
initializers.StartDatabaseConnect()
r := tests.RouteSetupTesting()
r.DELETE("/foobar/:id", controllers.DeleteFoobarByParamId)
contents := []byte(tests.MyFoobarEdited)
endpointPath := "/foobar/" + strconv.Itoa(tests.SharedTestID)
request, _ := http.NewRequest("DELETE", endpointPath, bytes.NewBuffer(contents))
response := httptest.NewRecorder()
r.ServeHTTP(response, request)
tests.SharedTestID = tests.GetIdFromHttpResponse(response)
assert.Equal(t, http.StatusAccepted, response.Code, "Not expected status code")
}
func TestEndpointFoobarMock(t *testing.T) {
gin.SetMode(gin.ReleaseMode)
r := tests.RouteSetupTesting()
r.GET("/foobar/mock", controllers.ShownFoobarMockToLearnTests)
request, _ := http.NewRequest("GET", "/foobar/mock", nil)
response := httptest.NewRecorder()
r.ServeHTTP(response, request)
assert.Equal(t, http.StatusOK, response.Code, "Not expected status code")
}
func TestEndpointFoobarMockContent(t *testing.T) {
r := tests.RouteSetupTesting()
r.GET("/foobar/mock", controllers.ShownFoobarMockToLearnTests)
request, _ := http.NewRequest("GET", "/foobar/mock", nil)
response := httptest.NewRecorder()
r.ServeHTTP(response, request)
mock := tests.MyFoobarString
body, _ := ioutil.ReadAll(response.Body)
assert.Equal(t, string(mock), string(body), "Not expected contents")
}