github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/edgeworkers/validations_test.go (about) 1 package edgeworkers 2 3 import ( 4 "bytes" 5 "context" 6 "errors" 7 "net/http" 8 "net/http/httptest" 9 "strings" 10 "testing" 11 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/require" 14 ) 15 16 func TestValidateBundle(t *testing.T) { 17 tests := map[string]struct { 18 params ValidateBundleRequest 19 responseStatus int 20 responseBody string 21 expectedPath string 22 expectedResponse *ValidateBundleResponse 23 withError error 24 }{ 25 "200 OK": { 26 params: ValidateBundleRequest{Bundle{strings.NewReader("a valid bundle")}}, 27 responseStatus: 200, 28 responseBody: ` 29 { 30 "errors": [], 31 "warnings": [] 32 }`, 33 expectedPath: "/edgeworkers/v1/validations", 34 expectedResponse: &ValidateBundleResponse{ 35 Errors: []ValidationIssue{}, 36 Warnings: []ValidationIssue{}, 37 }, 38 }, 39 "200 OK with invalid gzip format error": { 40 params: ValidateBundleRequest{Bundle{strings.NewReader("invalid bundle format")}}, 41 responseStatus: 200, 42 responseBody: ` 43 { 44 "errors": [ 45 { 46 "type": "INVALID_GZIP_FORMAT", 47 "message": "invalid GZIP file format" 48 } 49 ], 50 "warnings": [] 51 }`, 52 expectedPath: "/edgeworkers/v1/validations", 53 expectedResponse: &ValidateBundleResponse{ 54 Errors: []ValidationIssue{ 55 { 56 Type: "INVALID_GZIP_FORMAT", 57 Message: "invalid GZIP file format", 58 }, 59 }, 60 Warnings: []ValidationIssue{}, 61 }, 62 }, 63 "200 OK with expiring token warning": { 64 params: ValidateBundleRequest{Bundle{bytes.NewReader([]byte("bundle with expiring token"))}}, 65 responseStatus: 200, 66 responseBody: ` 67 { 68 "errors": [], 69 "warnings": [ 70 { 71 "type": "ACCESS_TOKEN_EXPIRING_SOON", 72 "message": "token expiring soon" 73 } 74 ] 75 }`, 76 expectedPath: "/edgeworkers/v1/validations", 77 expectedResponse: &ValidateBundleResponse{ 78 Errors: []ValidationIssue{}, 79 Warnings: []ValidationIssue{ 80 { 81 Type: "ACCESS_TOKEN_EXPIRING_SOON", 82 Message: "token expiring soon", 83 }, 84 }, 85 }, 86 }, "500 internal server error": { 87 params: ValidateBundleRequest{Bundle{bytes.NewReader([]byte("a valid bundle"))}}, 88 responseStatus: http.StatusInternalServerError, 89 responseBody: ` 90 { 91 "type": "/edgeworkers/error-types/edgeworkers-server-error", 92 "title": "An unexpected error has occurred.", 93 "detail": "Error processing request", 94 "instance": "/edgeworkers/error-instances/abc", 95 "status": 500, 96 "errorCode": "EW4303" 97 }`, 98 expectedPath: "/edgeworkers/v1/validations", 99 withError: &Error{ 100 Type: "/edgeworkers/error-types/edgeworkers-server-error", 101 Title: "An unexpected error has occurred.", 102 Detail: "Error processing request", 103 Instance: "/edgeworkers/error-instances/abc", 104 Status: 500, 105 ErrorCode: "EW4303", 106 }, 107 }, 108 "missing bundle reader": { 109 params: ValidateBundleRequest{}, 110 withError: ErrStructValidation, 111 }, 112 } 113 114 for name, test := range tests { 115 t.Run(name, func(t *testing.T) { 116 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 117 assert.Equal(t, test.expectedPath, r.URL.String()) 118 assert.Equal(t, http.MethodPost, r.Method) 119 w.WriteHeader(test.responseStatus) 120 _, err := w.Write([]byte(test.responseBody)) 121 assert.NoError(t, err) 122 })) 123 client := mockAPIClient(t, mockServer) 124 result, err := client.ValidateBundle(context.Background(), test.params) 125 if test.withError != nil { 126 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 127 return 128 } 129 require.NoError(t, err) 130 assert.Equal(t, test.expectedResponse, result) 131 }) 132 } 133 }