-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_test.go
69 lines (57 loc) · 1.85 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
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"net/http/httptest"
"testing"
)
func TestPostMethod(t *testing.T) {
gin.SetMode(gin.TestMode)
r := gin.Default()
r.POST("/", SamplePost)
// Create the mock request you'd like to test. Make sure the second argument
// here is the same as one of the routes you defined in the router setup
// block!
req, err := http.NewRequest(http.MethodPost, "/", nil)
if err != nil {
t.Fatalf("Couldn't create request: %v\n", err)
}
// Create a response recorder so you can inspect the response
w := httptest.NewRecorder()
// Perform the request
r.ServeHTTP(w, req)
fmt.Println(w.Body)
// Check to see if the response was what you expected
if w.Code == http.StatusOK {
t.Logf("Expected to get status %d is same ast %d\n", http.StatusOK, w.Code)
} else {
t.Fatalf("Expected to get status %d but instead got %d\n", http.StatusOK, w.Code)
}
}
func TestGetMethod(t *testing.T) {
// Switch to test mode so you don't get such noisy output
gin.SetMode(gin.TestMode)
// Setup your router, just like you did in your main function, and
// register your routes
r := gin.Default()
r.GET("/", SampleGet)
// Create the mock request you'd like to test. Make sure the second argument
// here is the same as one of the routes you defined in the router setup
// block!
req, err := http.NewRequest(http.MethodGet, "/", nil)
if err != nil {
t.Fatalf("Couldn't create request: %v\n", err)
}
// Create a response recorder so you can inspect the response
w := httptest.NewRecorder()
// Perform the request
r.ServeHTTP(w, req)
fmt.Println(w.Body)
// Check to see if the response was what you expected
if w.Code == http.StatusOK {
t.Logf("Expected to get status %d is same ast %d\n", http.StatusOK, w.Code)
} else {
t.Fatalf("Expected to get status %d but instead got %d\n", http.StatusOK, w.Code)
}
}