/
current_ip_test.go
49 lines (41 loc) · 1.01 KB
/
current_ip_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
package endpoints
import (
"github.com/julienschmidt/httprouter"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)
func TestCurrentIPEndpointSucceeds(t *testing.T) {
endpoint := NewCurrentIPEndpoint(func() (string, error) {
return "123.123.123.123", nil
})
resp := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/irrelevant", nil)
endpoint.CurrentIP(resp, req, httprouter.Params{})
assert.JSONEq(
t,
`{
"ip": "123.123.123.123"
}`,
resp.Body.String(),
)
}
func TestCurrentIPEndpointReturnsErrorWhenIPDetectionFails(t *testing.T) {
endpoint := NewCurrentIPEndpoint(func() (string, error) {
return "", errors.New("fake error")
})
resp := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/irrelevant", nil)
endpoint.CurrentIP(resp, req, httprouter.Params{})
// TODO: status code
assert.Equal(t, http.StatusInternalServerError, resp.Code)
assert.JSONEq(
t,
`{
"message": "fake error"
}`,
resp.Body.String(),
)
}