feat(platform): add amap reverse geocode for trajectory
This commit is contained in:
@@ -156,3 +156,91 @@ func TestAMapSecurityProxyRoutesKnownMapResources(t *testing.T) {
|
||||
t.Fatalf("unexpected proxy routing: %+v", seen)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAMapReverseGeocodeAPIUsesServerSideKey(t *testing.T) {
|
||||
var gotKey string
|
||||
var gotLocation string
|
||||
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
gotKey = r.URL.Query().Get("key")
|
||||
gotLocation = r.URL.Query().Get("location")
|
||||
if r.URL.Path != "/v3/geocode/regeo" {
|
||||
t.Fatalf("path = %q", r.URL.Path)
|
||||
}
|
||||
_, _ = w.Write([]byte(`{
|
||||
"status":"1",
|
||||
"info":"OK",
|
||||
"regeocode":{
|
||||
"formatted_address":"广东省广州市天河区测试路",
|
||||
"addressComponent":{
|
||||
"province":"广东省",
|
||||
"city":"广州市",
|
||||
"district":"天河区",
|
||||
"township":"五山街道",
|
||||
"adcode":"440106"
|
||||
}
|
||||
}
|
||||
}`))
|
||||
}))
|
||||
defer upstream.Close()
|
||||
|
||||
handler := withAMapReverseGeocodeAPI(http.NotFoundHandler(), config.Config{AMapAPIKey: "server-api-key"}, upstream.URL, http.DefaultClient)
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/map/reverse-geocode?longitude=113.1234567&latitude=23.7654321", nil)
|
||||
req.Header.Set("X-Trace-Id", "trace-map")
|
||||
handler.ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if gotKey != "server-api-key" || gotLocation != "113.123457,23.765432" {
|
||||
t.Fatalf("key=%q location=%q", gotKey, gotLocation)
|
||||
}
|
||||
var body struct {
|
||||
Data struct {
|
||||
Provider string `json:"provider"`
|
||||
Longitude float64 `json:"longitude"`
|
||||
Latitude float64 `json:"latitude"`
|
||||
FormattedAddress string `json:"formattedAddress"`
|
||||
Province string `json:"province"`
|
||||
City string `json:"city"`
|
||||
District string `json:"district"`
|
||||
Township string `json:"township"`
|
||||
Adcode string `json:"adcode"`
|
||||
} `json:"data"`
|
||||
TraceID string `json:"traceId"`
|
||||
}
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
|
||||
t.Fatalf("response should be JSON: %v body=%s", err, rec.Body.String())
|
||||
}
|
||||
if body.TraceID != "trace-map" || body.Data.Provider != "AMap" || body.Data.FormattedAddress != "广东省广州市天河区测试路" || body.Data.Adcode != "440106" {
|
||||
t.Fatalf("unexpected reverse geocode body: %+v", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAMapReverseGeocodeAPIRequiresServerSideKey(t *testing.T) {
|
||||
handler := withAMapReverseGeocodeAPI(http.NotFoundHandler(), config.Config{}, "https://example.com", http.DefaultClient)
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/map/reverse-geocode?longitude=113.1&latitude=23.1", nil)
|
||||
handler.ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if !strings.Contains(rec.Body.String(), "AMAP_API_UNCONFIGURED") {
|
||||
t.Fatalf("body should mention missing AMap API config: %s", rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestAMapReverseGeocodeAPIRejectsBadCoordinate(t *testing.T) {
|
||||
handler := withAMapReverseGeocodeAPI(http.NotFoundHandler(), config.Config{AMapAPIKey: "server-api-key"}, "https://example.com", http.DefaultClient)
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/map/reverse-geocode?longitude=0&latitude=0", nil)
|
||||
handler.ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if !strings.Contains(rec.Body.String(), "BAD_COORDINATE") {
|
||||
t.Fatalf("body should mention bad coordinate: %s", rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user