Skip to content

Commit

Permalink
fix: mutation original request issue #208 (#236)
Browse files Browse the repository at this point in the history
Co-authored-by: Kiryl Andruski <[email protected]>
Co-authored-by: alpha.wong <[email protected]>
  • Loading branch information
3 people authored Apr 27, 2020
1 parent 6abb271 commit 3faa067
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
5 changes: 3 additions & 2 deletions places.go
Original file line number Diff line number Diff line change
Expand Up @@ -757,10 +757,11 @@ func (r *PlaceAutocompleteRequest) params() url.Values {

var cf []string
for c, f := range r.Components {
fc := make([]string, len(f))
for i, v := range f {
f[i] = string(c) + ":" + v
fc[i] = string(c) + ":" + v
}
cf = append(cf, strings.Join(f, "|"))
cf = append(cf, strings.Join(fc, "|"))
}
if len(cf) > 0 {
q.Set("components", strings.Join(cf, "|"))
Expand Down
46 changes: 46 additions & 0 deletions places_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package maps
import (
"context"
"encoding/json"
"reflect"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -499,6 +500,51 @@ func TestPlaceAutocompleteMaximalRequestURL(t *testing.T) {
}
}

func TestPlaceAutocompleteWithMutipleComponentsWillWithoutMutation(t *testing.T) {
expectedQuery := "components=country%3AES%7Ccountry%3AAU%7Ccountry%3AHK&input=quay+resteraunt+sydney&key=AIzaNotReallyAnAPIKey&language=es&location=1%2C2&offset=5&radius=10000&types=geocode"

server := mockServerForQuery(expectedQuery, 200, `{"status":"OK"}"`)
defer server.s.Close()

c, _ := NewClient(WithAPIKey(apiKey), WithBaseURL(server.s.URL))

placeType, err := ParseAutocompletePlaceType("geocode")
if err != nil {
t.Errorf("Unexpected error in parsing place type: %v", err)
}

r := &PlaceAutocompleteRequest{
Input: "quay resteraunt sydney",
Offset: 5,
Location: &LatLng{1.0, 2.0},
Radius: 10000,
Language: "es",
Types: placeType,
Components: map[Component][]string{
ComponentCountry: {"ES", "AU", "HK"},
},
}

var copiedComponents = map[Component][]string{
ComponentCountry: {"ES", "AU", "HK"},
}

// copy the request
var originalReq = *r
originalReq.Components = copiedComponents

_, err = c.PlaceAutocomplete(context.Background(), r)

if eq := reflect.DeepEqual(*r, originalReq); !eq {
t.Errorf("Unexpected mutation at PlaceAutocompleteRequest")
}
if err != nil {
t.Errorf("Unexpected error in constructing request URL: %+v", err)
} else if server.successful != 1 {
t.Errorf("Got URL(s) %v, want %s", server.failed, expectedQuery)
}
}

func TestPlaceAutocompleteMissingInput(t *testing.T) {
c, _ := NewClient(WithAPIKey(apiKey))
r := &PlaceAutocompleteRequest{}
Expand Down

0 comments on commit 3faa067

Please sign in to comment.