@@ -15,9 +15,15 @@ import (
1515 v2 "github.com/m-lab/locate/api/v2"
1616 "github.com/m-lab/locate/proxy"
1717 "github.com/m-lab/locate/static"
18+ log "github.com/sirupsen/logrus"
1819 "gopkg.in/square/go-jose.v2/jwt"
1920)
2021
22+ func init () {
23+ // Disable most logs for unit tests.
24+ log .SetLevel (log .FatalLevel )
25+ }
26+
2127type fakeSigner struct {
2228 err error
2329}
@@ -52,6 +58,8 @@ func TestClient_TranslatedQuery(t *testing.T) {
5258 locator * fakeLocator
5359 project string
5460 latlon string
61+ header http.Header
62+ wantLatLon string
5563 wantKey string
5664 wantStatus int
5765 }{
@@ -68,14 +76,62 @@ func TestClient_TranslatedQuery(t *testing.T) {
6876 },
6977 wantStatus : http .StatusInternalServerError ,
7078 },
79+ {
80+ name : "error-corrupt-latlon" ,
81+ path : "ndt/ndt5" ,
82+ signer : & fakeSigner {},
83+ locator : & fakeLocator {
84+ targets : []v2.Target {{Machine : "mlab1-lga0t.measurement-lab.org" }},
85+ },
86+ header : http.Header {
87+ "X-AppEngine-CityLatLong" : []string {"corrupt-value" },
88+ },
89+ wantLatLon : "" ,
90+ wantKey : "ws://:3001/ndt_protocol" ,
91+ wantStatus : http .StatusOK ,
92+ },
7193 {
7294 name : "success-nearest-server" ,
7395 path : "ndt/ndt5" ,
7496 signer : & fakeSigner {},
7597 locator : & fakeLocator {
7698 targets : []v2.Target {{Machine : "mlab1-lga0t.measurement-lab.org" }},
7799 },
78- latlon : "40.3,-70.4" ,
100+ header : http.Header {
101+ "X-AppEngine-CityLatLong" : []string {"40.3,-70.4" },
102+ },
103+ wantLatLon : "40.3,-70.4" , // Client receives lat/lon provided by AppEngine.
104+ wantKey : "ws://:3001/ndt_protocol" ,
105+ wantStatus : http .StatusOK ,
106+ },
107+ {
108+ name : "success-nearest-server-using-region" ,
109+ path : "ndt/ndt5" ,
110+ signer : & fakeSigner {},
111+ locator : & fakeLocator {
112+ targets : []v2.Target {{Machine : "mlab1-lga0t.measurement-lab.org" }},
113+ },
114+ header : http.Header {
115+ "X-AppEngine-Country" : []string {"US" },
116+ "X-AppEngine-Region" : []string {"ny" },
117+ },
118+ wantLatLon : "43.19880000,-75.3242000" , // Region center.
119+ wantKey : "ws://:3001/ndt_protocol" ,
120+ wantStatus : http .StatusOK ,
121+ },
122+ {
123+ name : "success-nearest-server-using-country" ,
124+ path : "ndt/ndt5" ,
125+ signer : & fakeSigner {},
126+ locator : & fakeLocator {
127+ targets : []v2.Target {{Machine : "mlab1-lga0t.measurement-lab.org" }},
128+ },
129+ header : http.Header {
130+ "X-AppEngine-Region" : []string {"fake-region" },
131+ "X-AppEngine-Country" : []string {"US" },
132+ "X-AppEngine-CityLatLong" : []string {"0.000000,0.000000" },
133+ },
134+ wantLatLon : "37.09024,-95.712891" , // Country center.
79135 wantKey : "ws://:3001/ndt_protocol" ,
80136 wantStatus : http .StatusOK ,
81137 },
@@ -85,13 +141,13 @@ func TestClient_TranslatedQuery(t *testing.T) {
85141 c := NewClient (tt .project , tt .signer , tt .locator )
86142
87143 mux := http .NewServeMux ()
88- mux .HandleFunc ("/v2/query /" , c .TranslatedQuery )
144+ mux .HandleFunc ("/v2/nearest /" , c .TranslatedQuery )
89145 srv := httptest .NewServer (mux )
90146 defer srv .Close ()
91147
92- req , err := http .NewRequest (http .MethodGet , srv .URL + "/v2/query /" + tt .path , nil )
148+ req , err := http .NewRequest (http .MethodGet , srv .URL + "/v2/nearest /" + tt .path , nil )
93149 rtx .Must (err , "Failed to create request" )
94- req .Header . Set ( "X-AppEngine-CityLatLong" , tt .latlon )
150+ req .Header = tt .header
95151
96152 result := & v2.QueryResult {}
97153 resp , err := proxy .UnmarshalResponse (req , result )
@@ -106,6 +162,10 @@ func TestClient_TranslatedQuery(t *testing.T) {
106162 t .Errorf ("TranslatedQuery() wrong Content-Type header; got %s, want 'application/json'" ,
107163 resp .Header .Get ("Content-Type" ))
108164 }
165+ if resp .Header .Get ("X-Locate-ClientLatLon" ) != tt .wantLatLon {
166+ t .Errorf ("TranslatedQuery() wrong X-Locate-ClientLatLon header; got %s, want '%s'" ,
167+ resp .Header .Get ("X-Locate-ClientLatLon" ), tt .wantLatLon )
168+ }
109169 if result .Error != nil && result .Error .Status != tt .wantStatus {
110170 t .Errorf ("TranslatedQuery() wrong status; got %d, want %d" , result .Error .Status , tt .wantStatus )
111171 }
@@ -115,7 +175,6 @@ func TestClient_TranslatedQuery(t *testing.T) {
115175 if result .Results == nil && tt .wantStatus == http .StatusOK {
116176 t .Errorf ("TranslatedQuery() wrong status; got %d, want %d" , result .Error .Status , tt .wantStatus )
117177 }
118- // pretty.Print(result)
119178 if len (tt .locator .targets ) != len (result .Results ) {
120179 t .Errorf ("TranslateQuery() wrong result count; got %d, want %d" ,
121180 len (result .Results ), len (tt .locator .targets ))
0 commit comments