-
Notifications
You must be signed in to change notification settings - Fork 0
/
geonames_test.go
75 lines (66 loc) · 1.9 KB
/
geonames_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
package geonames_test
import (
"context"
"io"
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/qba73/geonames"
)
// newTestServer is a helper func that creates a test server with embedded URI validation.
func newTestServer(testFile, wantURI string, t *testing.T) *httptest.Server {
ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
gotReqURI := r.RequestURI
verifyURIs(wantURI, gotReqURI, t)
f, err := os.Open(testFile)
if err != nil {
t.Fatal(err)
}
defer f.Close()
_, err = io.Copy(rw, f)
if err != nil {
t.Fatal(err)
}
}))
return ts
}
// verifyURIs is a test helper function that verifies if provided URIs are the same.
func verifyURIs(wanturi, goturi string, t *testing.T) {
wantU, err := url.Parse(wanturi)
if err != nil {
t.Fatalf("error parsing URL %q, %v", wanturi, err)
}
gotU, err := url.Parse(goturi)
if err != nil {
t.Fatalf("error parsing URL %q, %v", wanturi, err)
}
// Verify if paths of both URIs are the same.
if wantU.Path != gotU.Path {
t.Fatalf("want %q, got %q", wantU.Path, gotU.Path)
}
wantQuery, err := url.ParseQuery(wantU.RawQuery)
if err != nil {
t.Fatal(err)
}
gotQuery, err := url.ParseQuery(gotU.RawQuery)
if err != nil {
t.Fatal(err)
}
// Verify if query parameters match in both, got and want URIs.
if !cmp.Equal(wantQuery, gotQuery) {
t.Fatalf("URIs are not equal, \n%s\n", cmp.Diff(wantQuery, gotQuery))
}
}
func TestWikipediaResolvesGeoNameOnValidInput(t *testing.T) {
t.Parallel()
testFile := "testdata/response-geoname-wikipedia-single.json"
wantReqURI := "/wikipediaSearchJSON?q=Castlebar&title=Castlebar&countryCode=IE&maxRows=1&username=DummyUser"
ts := newTestServer(testFile, wantReqURI, t)
defer ts.Close()
name, country := "Castlebar", "IE"
c := geonames.NewClient("DummyUser")
c.GetPlace(context.Background(), name, country, 1)
}