github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/cloudwrapper/locations_test.go (about) 1 package cloudwrapper 2 3 import ( 4 "context" 5 "errors" 6 "net/http" 7 "net/http/httptest" 8 "testing" 9 10 "github.com/tj/assert" 11 ) 12 13 func TestCloudwrapper_ListLocations(t *testing.T) { 14 tests := map[string]struct { 15 responseStatus int 16 expectedPath string 17 responseBody string 18 expectedResponse *ListLocationResponse 19 withError func(*testing.T, error) 20 }{ 21 "200 OK": { 22 responseStatus: http.StatusOK, 23 expectedPath: "/cloud-wrapper/v1/locations", 24 responseBody: `{ 25 "locations": [ 26 { 27 "locationId": 1, 28 "locationName": "US East", 29 "trafficTypes": [ 30 { 31 "trafficTypeId": 1, 32 "trafficType": "TEST_TT1", 33 "mapName": "cw-essl-use" 34 }, 35 { 36 "trafficTypeId": 2, 37 "trafficType": "TEST_TT2", 38 "mapName": "cw-s-use-live" 39 } 40 ], 41 "multiCdnLocationId": "0123" 42 }, 43 { 44 "locationId": 2, 45 "locationName": "US West", 46 "trafficTypes": [ 47 { 48 "trafficTypeId": 3, 49 "trafficType": "TEST_TT1", 50 "mapName": "cw-essl-use" 51 }, 52 { 53 "trafficTypeId": 4, 54 "trafficType": "TEST_TT2", 55 "mapName": "cw-s-use-live" 56 } 57 ], 58 "multiCdnLocationId": "4567" 59 } 60 ]}`, 61 expectedResponse: &ListLocationResponse{ 62 Locations: []Location{ 63 { 64 LocationID: 1, 65 LocationName: "US East", 66 TrafficTypes: []TrafficTypeItem{ 67 { 68 TrafficTypeID: 1, 69 TrafficType: "TEST_TT1", 70 MapName: "cw-essl-use", 71 }, 72 { 73 TrafficTypeID: 2, 74 TrafficType: "TEST_TT2", 75 MapName: "cw-s-use-live", 76 }, 77 }, 78 MultiCDNLocationID: "0123", 79 }, 80 { 81 LocationID: 2, 82 LocationName: "US West", 83 TrafficTypes: []TrafficTypeItem{ 84 { 85 TrafficTypeID: 3, 86 TrafficType: "TEST_TT1", 87 MapName: "cw-essl-use", 88 }, 89 { 90 TrafficTypeID: 4, 91 TrafficType: "TEST_TT2", 92 MapName: "cw-s-use-live", 93 }, 94 }, 95 MultiCDNLocationID: "4567", 96 }, 97 }, 98 }, 99 }, 100 "500 internal server error": { 101 responseStatus: http.StatusInternalServerError, 102 expectedPath: "/cloud-wrapper/v1/locations", 103 responseBody: ` 104 { 105 "type": "internal_error", 106 "title": "Internal Server Error", 107 "detail": "Error processing request", 108 "status": 500 109 }`, 110 withError: func(t *testing.T, err error) { 111 want := &Error{ 112 Type: "internal_error", 113 Title: "Internal Server Error", 114 Detail: "Error processing request", 115 Status: http.StatusInternalServerError, 116 } 117 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 118 }, 119 }, 120 } 121 for name, test := range tests { 122 t.Run(name, func(t *testing.T) { 123 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 124 assert.Equal(t, test.expectedPath, r.URL.String()) 125 assert.Equal(t, http.MethodGet, r.Method) 126 w.WriteHeader(test.responseStatus) 127 _, err := w.Write([]byte(test.responseBody)) 128 assert.NoError(t, err) 129 })) 130 client := mockAPIClient(t, mockServer) 131 users, err := client.ListLocations(context.Background()) 132 if test.withError != nil { 133 test.withError(t, err) 134 return 135 } 136 assert.NoError(t, err) 137 assert.Equal(t, test.expectedResponse, users) 138 }) 139 } 140 }