-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain_test.go
66 lines (56 loc) · 1.54 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
package main
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)
func TestGetIP(t *testing.T) {
// Setup
e := echo.New()
req := httptest.NewRequest(echo.GET, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
// Assertions
if assert.NoError(t, handleRequest(c)) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "192.0.2.1", rec.Body.String())
}
}
func TestGetIPJSON(t *testing.T) {
// Setup
e := echo.New()
req := httptest.NewRequest(echo.GET, "/?format=json", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
// Assertions
if assert.NoError(t, handleRequest(c)) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "{\"ip\":\"192.0.2.1\"}\n", rec.Body.String())
}
}
func TestGetJSONP(t *testing.T) {
// Setup
e := echo.New()
req := httptest.NewRequest(echo.GET, "/?format=jsonp&callback=cb", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
// Assertions
if assert.NoError(t, handleRequest(c)) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "cb({\"ip\":\"192.0.2.1\"}\n);", rec.Body.String())
}
}
func TestGetXML(t *testing.T) {
// Setup
e := echo.New()
req := httptest.NewRequest(echo.GET, "/?format=xml", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
// Assertions
if assert.NoError(t, handleRequest(c)) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<IP>192.0.2.1</IP>", rec.Body.String())
}
}