|
| 1 | +package httpsms |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/NdoleStudio/httpsms-go/internal/helpers" |
| 10 | + "github.com/NdoleStudio/httpsms-go/internal/stubs" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func TestPhoneAPIKeyService_Store(t *testing.T) { |
| 15 | + // Setup |
| 16 | + t.Parallel() |
| 17 | + |
| 18 | + // Arrange |
| 19 | + apiKey := "test-api-key" |
| 20 | + server := helpers.MakeTestServer(http.StatusOK, stubs.PhoneAPIKeyStoreResponse()) |
| 21 | + client := New(WithBaseURL(server.URL), WithAPIKey(apiKey)) |
| 22 | + |
| 23 | + // Act |
| 24 | + phoneAPIKey, response, err := client.PhoneAPIKeys.Store(context.Background(), &PhoneAPIKeyStoreParams{ |
| 25 | + Name: "test-key", |
| 26 | + }) |
| 27 | + |
| 28 | + // Assert |
| 29 | + assert.Nil(t, err) |
| 30 | + assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
| 31 | + |
| 32 | + jsonContent, _ := json.Marshal(phoneAPIKey) |
| 33 | + assert.JSONEq(t, string(stubs.PhoneAPIKeyStoreResponse()), string(jsonContent)) |
| 34 | + |
| 35 | + // Teardown |
| 36 | + server.Close() |
| 37 | +} |
| 38 | + |
| 39 | +func TestPhoneAPIKeyService_StoreWithError(t *testing.T) { |
| 40 | + // Setup |
| 41 | + t.Parallel() |
| 42 | + |
| 43 | + // Arrange |
| 44 | + apiKey := "test-api-key" |
| 45 | + server := helpers.MakeTestServer(http.StatusInternalServerError, stubs.HttpInternalServerErrorResponse()) |
| 46 | + client := New(WithBaseURL(server.URL), WithAPIKey(apiKey)) |
| 47 | + |
| 48 | + // Act |
| 49 | + _, response, err := client.PhoneAPIKeys.Store(context.Background(), &PhoneAPIKeyStoreParams{ |
| 50 | + Name: "test-key", |
| 51 | + }) |
| 52 | + |
| 53 | + // Assert |
| 54 | + assert.NotNil(t, err) |
| 55 | + assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode) |
| 56 | + assert.Equal(t, string(stubs.HttpInternalServerErrorResponse()), string(*response.Body)) |
| 57 | + |
| 58 | + // Teardown |
| 59 | + server.Close() |
| 60 | +} |
| 61 | + |
| 62 | +func TestPhoneAPIKeyService_StoreRequest(t *testing.T) { |
| 63 | + // Setup |
| 64 | + t.Parallel() |
| 65 | + |
| 66 | + // Arrange |
| 67 | + apiKey := "test-api-key" |
| 68 | + var capturedRequest http.Request |
| 69 | + server := helpers.MakeRequestCapturingTestServer(http.StatusOK, stubs.PhoneAPIKeyStoreResponse(), &capturedRequest) |
| 70 | + client := New(WithBaseURL(server.URL), WithAPIKey(apiKey)) |
| 71 | + |
| 72 | + // Act |
| 73 | + _, _, err := client.PhoneAPIKeys.Store(context.Background(), &PhoneAPIKeyStoreParams{ |
| 74 | + Name: "test-key", |
| 75 | + }) |
| 76 | + |
| 77 | + // Assert |
| 78 | + assert.Nil(t, err) |
| 79 | + assert.Equal(t, http.MethodPost, capturedRequest.Method) |
| 80 | + assert.Equal(t, "/v1/phone-api-keys/", capturedRequest.URL.Path) |
| 81 | + assert.Equal(t, "application/json", capturedRequest.Header.Get("Content-Type")) |
| 82 | + assert.Equal(t, apiKey, capturedRequest.Header.Get("x-api-key")) |
| 83 | + |
| 84 | + // Teardown |
| 85 | + server.Close() |
| 86 | +} |
0 commit comments