-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeocoding_batch_reverse_test.go
More file actions
130 lines (120 loc) · 4.02 KB
/
Copy pathgeocoding_batch_reverse_test.go
File metadata and controls
130 lines (120 loc) · 4.02 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package mapbox_test
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
mapbox "github.com/way-platform/mapbox-go"
)
func TestBatchReverseGeocode_JSONBody(t *testing.T) {
var gotBody []map[string]any
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if ct := r.Header.Get("Content-Type"); ct != "application/json" {
t.Errorf("Content-Type = %q, want application/json", ct)
}
if tok := r.URL.Query().Get("access_token"); tok != "batch-token" {
t.Errorf("access_token = %q in URL", tok)
}
if err := json.NewDecoder(r.Body).Decode(&gotBody); err != nil {
t.Errorf("decode request body: %v", err)
}
result := map[string]any{
"batch": []map[string]any{
{"type": "FeatureCollection", "features": []any{}},
{"type": "FeatureCollection", "features": []any{}},
},
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(result); err != nil {
t.Errorf("encode response: %v", err)
}
}))
defer srv.Close()
client := mapbox.NewClient(mapbox.WithAccessToken("batch-token"), mapbox.WithBaseURL(srv.URL))
results, err := client.BatchReverseGeocode(context.Background(), &mapbox.BatchReverseGeocodeRequest{
Queries: []mapbox.ReverseGeocodeQuery{
{Longitude: 13.4, Latitude: 52.5},
{Longitude: -0.12, Latitude: 51.5},
},
})
if err != nil {
t.Fatalf("BatchReverseGeocode error: %v", err)
}
if len(results) != 2 {
t.Errorf("len(results) = %d, want 2", len(results))
}
// Verify request body format.
if len(gotBody) != 2 {
t.Fatalf("len(body) = %d, want 2", len(gotBody))
}
for i, q := range gotBody {
if types, ok := q["types"].(string); !ok || types != "address" {
t.Errorf("query[%d].types = %v, want \"address\"", i, q["types"])
}
}
if gotBody[0]["longitude"] != 13.4 {
t.Errorf("query[0].longitude = %v, want 13.4", gotBody[0]["longitude"])
}
}
func TestBatchReverseGeocode_PermanentParam(t *testing.T) {
tests := []struct {
name string
permanent bool
wantParam string
}{
{"temporary omits param", false, ""},
{"permanent sets param", true, "true"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var gotParam string
var gotBody []map[string]any
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotParam = r.URL.Query().Get("permanent")
if err := json.NewDecoder(r.Body).Decode(&gotBody); err != nil {
t.Errorf("decode body: %v", err)
}
result := map[string]any{"batch": []map[string]any{{"type": "FeatureCollection", "features": []any{}}}}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(result); err != nil {
t.Errorf("encode response: %v", err)
}
}))
defer srv.Close()
client := mapbox.NewClient(mapbox.WithAccessToken("t"), mapbox.WithBaseURL(srv.URL))
_, err := client.BatchReverseGeocode(context.Background(), &mapbox.BatchReverseGeocodeRequest{
Queries: []mapbox.ReverseGeocodeQuery{{Longitude: 13.4, Latitude: 52.5}},
Permanent: tc.permanent,
})
if err != nil {
t.Fatalf("BatchReverseGeocode error: %v", err)
}
if gotParam != tc.wantParam {
t.Errorf("permanent param = %q, want %q", gotParam, tc.wantParam)
}
// permanent must not appear in the JSON body - it's a query param only
if len(gotBody) > 0 {
if _, ok := gotBody[0]["permanent"]; ok {
t.Errorf("permanent field must not appear in request body")
}
}
})
}
}
func TestBatchReverseGeocode_LimitEnforcement(t *testing.T) {
client := mapbox.NewClient(mapbox.WithAccessToken("t"))
_, err := client.BatchReverseGeocode(context.Background(), &mapbox.BatchReverseGeocodeRequest{
Queries: []mapbox.ReverseGeocodeQuery{},
})
if err == nil {
t.Error("expected error for empty queries")
}
queries := make([]mapbox.ReverseGeocodeQuery, 1001)
_, err = client.BatchReverseGeocode(context.Background(), &mapbox.BatchReverseGeocodeRequest{
Queries: queries,
})
if err == nil {
t.Error("expected error for > 1000 queries")
}
}