github.com/m-lab/locate@v0.17.6/clientgeo/appengine_test.go (about)

     1  // Package clientgeo supports interfaces to different data sources to help
     2  // identify client geo location for server selection.
     3  package clientgeo
     4  
     5  import (
     6  	"context"
     7  	"net/http"
     8  	"net/http/httptest"
     9  	"reflect"
    10  	"testing"
    11  )
    12  
    13  func TestAppEngineLocator_Locate(t *testing.T) {
    14  	type args struct {
    15  	}
    16  	tests := []struct {
    17  		name       string
    18  		useHeaders map[string]string
    19  		want       *Location
    20  		wantErr    bool
    21  	}{
    22  		{
    23  			name: "success-using-latlong",
    24  			useHeaders: map[string]string{
    25  				"X-AppEngine-CityLatLong": "40.3,-70.4",
    26  			},
    27  			want: &Location{
    28  				Latitude:  "40.3",
    29  				Longitude: "-70.4",
    30  				Headers: http.Header{
    31  					hLocateClientlatlon:       []string{"40.3,-70.4"},
    32  					hLocateClientlatlonMethod: []string{"appengine-latlong"},
    33  				},
    34  			},
    35  		},
    36  		{
    37  			name:       "error-missing-country",
    38  			useHeaders: map[string]string{}, // none.
    39  			want: &Location{
    40  				Headers: http.Header{
    41  					hLocateClientlatlonMethod: []string{"appengine-none"},
    42  				},
    43  			},
    44  			wantErr: true,
    45  		},
    46  		{
    47  			name: "success-using-region",
    48  			useHeaders: map[string]string{
    49  				"X-AppEngine-Country": "US",
    50  				"X-AppEngine-Region":  "NY",
    51  			},
    52  			want: &Location{
    53  				Latitude:  "43.19880000",
    54  				Longitude: "-75.3242000",
    55  				Headers: http.Header{
    56  					hLocateClientlatlonMethod: []string{"appengine-region"},
    57  					hLocateClientlatlon:       []string{"43.19880000,-75.3242000"},
    58  				},
    59  			},
    60  		},
    61  		{
    62  			name: "success-ignore-latlong-use-region",
    63  			useHeaders: map[string]string{
    64  				"X-AppEngine-CityLatLong": "0.000000,0.000000", // some IPs receive a "null" latlon, when region and country are valid.
    65  				"X-AppEngine-Country":     "US",
    66  				"X-AppEngine-Region":      "NY",
    67  			},
    68  			want: &Location{
    69  				Latitude:  "43.19880000",
    70  				Longitude: "-75.3242000",
    71  				Headers: http.Header{
    72  					hLocateClientlatlonMethod: []string{"appengine-region"},
    73  					hLocateClientlatlon:       []string{"43.19880000,-75.3242000"},
    74  				},
    75  			},
    76  		},
    77  		{
    78  			name: "success-using-country",
    79  			useHeaders: map[string]string{
    80  				"X-AppEngine-Country": "US",
    81  			},
    82  			want: &Location{
    83  				Latitude:  "37.09024",
    84  				Longitude: "-95.712891",
    85  				Headers: http.Header{
    86  					hLocateClientlatlonMethod: []string{"appengine-country"},
    87  					hLocateClientlatlon:       []string{"37.09024,-95.712891"},
    88  				},
    89  			},
    90  		},
    91  	}
    92  	for _, tt := range tests {
    93  		t.Run(tt.name, func(t *testing.T) {
    94  			sl := NewAppEngineLocator()
    95  			sl.Reload(context.Background()) // completes code coverage.
    96  			req := httptest.NewRequest(http.MethodGet, "/whatever", nil)
    97  			for key, value := range tt.useHeaders {
    98  				req.Header.Set(key, value)
    99  			}
   100  			got, err := sl.Locate(req)
   101  			if (err != nil) != tt.wantErr {
   102  				t.Errorf("AppEngineLocator.Locate() error = %v, wantErr %v", err, tt.wantErr)
   103  				return
   104  			}
   105  			if !reflect.DeepEqual(got, tt.want) {
   106  				t.Errorf("AppEngineLocator.Locate() = %v, want %v", got, tt.want)
   107  			}
   108  		})
   109  	}
   110  }