echoip/api/api_test.go

149 lines
3.6 KiB
Go
Raw Normal View History

2015-09-17 20:57:27 +02:00
package api
import (
2016-04-15 20:14:16 +02:00
"encoding/json"
2015-09-17 20:57:27 +02:00
"io/ioutil"
2015-09-18 17:13:14 +02:00
"log"
2015-09-17 20:57:27 +02:00
"net"
"net/http"
"net/http/httptest"
2015-09-29 20:39:21 +02:00
"strings"
2015-09-17 20:57:27 +02:00
"testing"
)
2015-09-29 20:39:21 +02:00
func newTestAPI() *API {
return &API{
lookupAddr: func(string) ([]string, error) {
return []string{"localhost"}, nil
},
lookupCountry: func(ip net.IP) (string, error) {
return "Elbonia", nil
},
ipFromRequest: func(*http.Request) (net.IP, error) {
return net.ParseIP("127.0.0.1"), nil
},
2016-04-15 20:52:15 +02:00
testPort: func(net.IP, uint64) error {
return nil
},
2015-09-29 20:39:21 +02:00
}
}
2015-09-18 17:13:14 +02:00
func httpGet(url string, json bool, userAgent string) (string, int, error) {
2015-09-17 20:57:27 +02:00
r, err := http.NewRequest("GET", url, nil)
if err != nil {
2015-09-18 17:13:14 +02:00
return "", 0, err
2015-09-17 20:57:27 +02:00
}
if json {
r.Header.Set("Accept", "application/json")
}
r.Header.Set("User-Agent", userAgent)
res, err := http.DefaultClient.Do(r)
if err != nil {
2015-09-18 17:13:14 +02:00
return "", 0, err
2015-09-17 20:57:27 +02:00
}
defer res.Body.Close()
data, err := ioutil.ReadAll(res.Body)
if err != nil {
2015-09-18 17:13:14 +02:00
return "", 0, err
2015-09-17 20:57:27 +02:00
}
2015-09-18 17:13:14 +02:00
return string(data), res.StatusCode, nil
2015-09-17 20:57:27 +02:00
}
func TestGetIP(t *testing.T) {
2016-04-16 09:18:21 +02:00
log.SetOutput(ioutil.Discard)
2016-04-15 20:14:16 +02:00
toJSON := func(r Response) string {
b, err := json.Marshal(r)
if err != nil {
t.Fatal(err)
}
return string(b)
2015-09-17 20:57:27 +02:00
}
2015-09-29 20:39:21 +02:00
s := httptest.NewServer(newTestAPI().Handlers())
2015-09-17 20:57:27 +02:00
var tests = []struct {
url string
json bool
out string
userAgent string
2015-09-18 17:13:14 +02:00
status int
2015-09-17 20:57:27 +02:00
}{
2015-09-18 17:13:14 +02:00
{s.URL, false, "127.0.0.1\n", "curl/7.26.0", 200},
{s.URL, false, "127.0.0.1\n", "Wget/1.13.4 (linux-gnu)", 200},
{s.URL, false, "127.0.0.1\n", "fetch libfetch/2.0", 200},
2016-02-23 00:02:03 +01:00
{s.URL, false, "127.0.0.1\n", "Go 1.1 package http", 200},
{s.URL, false, "127.0.0.1\n", "Go-http-client/1.1", 200},
{s.URL, false, "127.0.0.1\n", "Go-http-client/2.0", 200},
2016-04-16 09:18:21 +02:00
{s.URL + "/country", false, "Elbonia\n", "curl/7.26.0", 200},
2016-04-15 20:14:16 +02:00
{s.URL, true, toJSON(Response{IP: net.ParseIP("127.0.0.1"), Country: "Elbonia", Hostname: "localhost"}), "", 200},
{s.URL + "/foo", false, "404 page not found", "curl/7.26.0", 404},
{s.URL + "/foo", true, "{\"error\":\"404 page not found\"}", "curl/7.26.0", 404},
2015-09-17 20:57:27 +02:00
}
2015-09-18 17:42:43 +02:00
2015-09-17 20:57:27 +02:00
for _, tt := range tests {
2015-09-18 17:13:14 +02:00
out, status, err := httpGet(tt.url, tt.json, tt.userAgent)
2015-09-17 20:57:27 +02:00
if err != nil {
t.Fatal(err)
}
2015-09-18 17:13:14 +02:00
if status != tt.status {
t.Errorf("Expected %d, got %d", tt.status, status)
}
2015-09-17 20:57:27 +02:00
if out != tt.out {
t.Errorf("Expected %q, got %q", tt.out, out)
}
}
}
2015-09-29 20:39:21 +02:00
func TestGetIPWithoutReverse(t *testing.T) {
log.SetOutput(ioutil.Discard)
api := newTestAPI()
s := httptest.NewServer(api.Handlers())
out, _, err := httpGet(s.URL, false, "curl/7.26.0")
if err != nil {
t.Fatal(err)
}
2016-04-15 20:48:52 +02:00
if key := "hostname"; strings.Contains(out, key) {
t.Errorf("Expected response to not key %q", key)
2015-09-29 20:39:21 +02:00
}
}
2015-09-17 20:57:27 +02:00
func TestIPFromRequest(t *testing.T) {
var tests = []struct {
in *http.Request
out net.IP
}{
{&http.Request{RemoteAddr: "1.3.3.7:9999"}, net.ParseIP("1.3.3.7")},
{&http.Request{Header: http.Header{"X-Real-Ip": []string{"1.3.3.7"}}}, net.ParseIP("1.3.3.7")},
}
for _, tt := range tests {
ip, err := ipFromRequest(tt.in)
if err != nil {
t.Fatal(err)
}
if !ip.Equal(tt.out) {
t.Errorf("Expected %s, got %s", tt.out, ip)
}
}
}
func TestCLIMatcher(t *testing.T) {
browserUserAgent := "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) " +
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.28 " +
"Safari/537.36"
var tests = []struct {
in string
out bool
}{
{"curl/7.26.0", true},
{"Wget/1.13.4 (linux-gnu)", true},
{"fetch libfetch/2.0", true},
2016-04-15 20:19:14 +02:00
{"HTTPie/0.9.3", true},
2015-09-17 20:57:27 +02:00
{browserUserAgent, false},
}
for _, tt := range tests {
r := &http.Request{Header: http.Header{"User-Agent": []string{tt.in}}}
if got := cliMatcher(r, nil); got != tt.out {
t.Errorf("Expected %t, got %t for %q", tt.out, got, tt.in)
}
}
}