diff --git a/international-autocomplete-api/client.go b/international-autocomplete-api/client.go index ef0fb49..75a5a3d 100644 --- a/international-autocomplete-api/client.go +++ b/international-autocomplete-api/client.go @@ -45,11 +45,11 @@ func deserializeResponse(response []byte, lookup *Lookup) error { } func buildRequest(lookup *Lookup) *http.Request { - var addressURL = "" + var addressID = "" if len(lookup.AddressID) > 0 { - addressURL = "/" + lookup.AddressID + addressID = "/" + lookup.AddressID } - request, _ := http.NewRequest("GET", suggestURL+addressURL, nil) // We control the method and the URL. This is safe. + request, _ := http.NewRequest("GET", suggestURL+addressID, nil) // We control the method and the URL. This is safe. query := request.URL.Query() lookup.populate(query) request.URL.RawQuery = query.Encode() diff --git a/international-autocomplete-api/client_test.go b/international-autocomplete-api/client_test.go index f4f9cea..ff94baf 100644 --- a/international-autocomplete-api/client_test.go +++ b/international-autocomplete-api/client_test.go @@ -36,21 +36,20 @@ func (f *ClientFixture) TestAddressLookupSerializedAndSentWithContext__ResponseS "street": "1", "locality": "2", "administrative_area": "3", - "super_administrative_area": "4", - "postal_code": "5", - "country_iso3": "6" + "postal_code": "4", + "country_iso3": "5" }, { - "street": "7", - "locality": "8", - "administrative_area": "9", - "super_administrative_area": "10", - "postal_code": "11", - "country_iso3": "12" + "street": "6", + "locality": "7", + "administrative_area": "8", + "postal_code": "9", + "country_iso3": "10" } ] }` f.input.Search = "42" + f.input.Country = "FRA" ctx := context.WithValue(context.Background(), "key", "value") err := f.client.SendLookupWithContext(ctx, f.input) @@ -60,25 +59,23 @@ func (f *ClientFixture) TestAddressLookupSerializedAndSentWithContext__ResponseS f.So(f.sender.request.Method, should.Equal, "GET") f.So(f.sender.request.URL.Path, should.Equal, suggestURL) f.So(f.sender.request.URL.Query().Get("search"), should.Equal, "42") - f.So(f.sender.request.URL.String(), should.Equal, suggestURL+"?distance=5&max_results=5&search=42") + f.So(f.sender.request.URL.String(), should.Equal, suggestURL+"?country=FRA&distance=5&max_results=5&search=42") f.So(f.sender.request.Context(), should.Resemble, ctx) f.So(f.input.Result, should.Resemble, &Result{Candidates: []*Candidate{ { - Street: "1", - Locality: "2", - AdministrativeArea: "3", - SuperAdministrativeArea: "4", - PostalCode: "5", - CountryIso3: "6", + Street: "1", + Locality: "2", + AdministrativeArea: "3", + PostalCode: "4", + CountryIso3: "5", }, { - Street: "7", - Locality: "8", - AdministrativeArea: "9", - SuperAdministrativeArea: "10", - PostalCode: "11", - CountryIso3: "12", + Street: "6", + Locality: "7", + AdministrativeArea: "8", + PostalCode: "9", + CountryIso3: "10", }, }}) } @@ -102,6 +99,7 @@ func (f *ClientFixture) TestSenderErrorPreventsDeserialization() { {"text": "3"} ]}` // would be deserialized if not for the err (above) f.input.Search = "HI" + f.input.Country = "FRA" err := f.client.SendLookup(f.input) @@ -112,6 +110,7 @@ func (f *ClientFixture) TestSenderErrorPreventsDeserialization() { func (f *ClientFixture) TestDeserializationErrorPreventsDeserialization() { f.sender.response = `I can't haz JSON` f.input.Search = "HI" + f.input.Country = "FRA" err := f.client.SendLookup(f.input) @@ -119,6 +118,20 @@ func (f *ClientFixture) TestDeserializationErrorPreventsDeserialization() { f.So(f.input.Result, should.BeNil) } +func (f *ClientFixture) TestAddressIDAppendsToURL() { + f.input.Country = "FRA" + f.input.AddressID = "thisisid" + + err := f.client.SendLookup(f.input) + + f.So(err, should.NotBeNil) + + f.So(f.sender.request, should.NotBeNil) + f.So(f.sender.request.Method, should.Equal, "GET") + f.So(f.sender.request.URL.Path, should.Equal, suggestURL+"/"+f.input.AddressID) + f.So(f.sender.request.URL.String(), should.Equal, suggestURL+"/"+f.input.AddressID+"?country=FRA&distance=5&max_results=5") +} + ////////////////////////////////////////////////////////////////// type FakeSender struct {