github.com/akamai/AkamaiOPEN-edgegrid-golang/v2@v2.17.0/pkg/appsec/api_endpoints_test.go (about)

     1  package appsec
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"errors"
     7  	"net/http"
     8  	"net/http/httptest"
     9  	"testing"
    10  
    11  	"github.com/akamai/AkamaiOPEN-edgegrid-golang/v2/pkg/session"
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestAppSec_ListApiEndpoints(t *testing.T) {
    17  
    18  	result := GetApiEndpointsResponse{}
    19  
    20  	respData := compactJSON(loadFixtureBytes("testdata/TestApiEndpoints/ApiEndpoints.json"))
    21  	json.Unmarshal([]byte(respData), &result)
    22  
    23  	tests := map[string]struct {
    24  		params           GetApiEndpointsRequest
    25  		responseStatus   int
    26  		responseBody     string
    27  		expectedPath     string
    28  		expectedResponse *GetApiEndpointsResponse
    29  		withError        error
    30  		headers          http.Header
    31  	}{
    32  		"200 OK": {
    33  			params: GetApiEndpointsRequest{
    34  				ConfigID: 43253,
    35  				Version:  15,
    36  			},
    37  			headers: http.Header{
    38  				"Content-Type": []string{"application/json"},
    39  			},
    40  			responseStatus:   http.StatusOK,
    41  			responseBody:     string(respData),
    42  			expectedPath:     "/appsec/v1/configs/43253/versions/15/api-endpoints",
    43  			expectedResponse: &result,
    44  		},
    45  		"500 internal server error": {
    46  			params: GetApiEndpointsRequest{
    47  				ConfigID: 43253,
    48  				Version:  15,
    49  			},
    50  			headers:        http.Header{},
    51  			responseStatus: http.StatusInternalServerError,
    52  			responseBody: `
    53  {
    54      "type": "internal_error",
    55      "title": "Internal Server Error",
    56      "detail": "Error fetching ApiEndpoints",
    57      "status": 500
    58  }`,
    59  			expectedPath: "/appsec/v1/configs/43253/versions/15/api-endpoints",
    60  			withError: &Error{
    61  				Type:       "internal_error",
    62  				Title:      "Internal Server Error",
    63  				Detail:     "Error fetching ApiEndpoints",
    64  				StatusCode: http.StatusInternalServerError,
    65  			},
    66  		},
    67  	}
    68  
    69  	for name, test := range tests {
    70  		t.Run(name, func(t *testing.T) {
    71  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    72  				assert.Equal(t, test.expectedPath, r.URL.String())
    73  				assert.Equal(t, http.MethodGet, r.Method)
    74  				w.WriteHeader(test.responseStatus)
    75  				_, err := w.Write([]byte(test.responseBody))
    76  				assert.NoError(t, err)
    77  			}))
    78  			client := mockAPIClient(t, mockServer)
    79  			result, err := client.GetApiEndpoints(
    80  				session.ContextWithOptions(
    81  					context.Background(),
    82  					session.WithContextHeaders(test.headers),
    83  				),
    84  				test.params)
    85  			if test.withError != nil {
    86  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
    87  				return
    88  			}
    89  			require.NoError(t, err)
    90  			assert.Equal(t, test.expectedResponse, result)
    91  		})
    92  	}
    93  }
    94  
    95  // Test ApiEndpoints
    96  func TestAppSec_GetApiEndpoints(t *testing.T) {
    97  
    98  	result := GetApiEndpointsResponse{}
    99  
   100  	respData := compactJSON(loadFixtureBytes("testdata/TestApiEndpoints/ApiEndpoints.json"))
   101  	json.Unmarshal([]byte(respData), &result)
   102  
   103  	tests := map[string]struct {
   104  		params           GetApiEndpointsRequest
   105  		responseStatus   int
   106  		responseBody     string
   107  		expectedPath     string
   108  		expectedResponse *GetApiEndpointsResponse
   109  		withError        error
   110  	}{
   111  		"200 OK": {
   112  			params: GetApiEndpointsRequest{
   113  				ConfigID: 43253,
   114  				Version:  15,
   115  			},
   116  			responseStatus:   http.StatusOK,
   117  			responseBody:     respData,
   118  			expectedPath:     "/appsec/v1/configs/43253/versions/15/api-endpoints",
   119  			expectedResponse: &result,
   120  		},
   121  		"500 internal server error": {
   122  			params: GetApiEndpointsRequest{
   123  				ConfigID: 43253,
   124  				Version:  15,
   125  			},
   126  			responseStatus: http.StatusInternalServerError,
   127  			responseBody: (`
   128  {
   129      "type": "internal_error",
   130      "title": "Internal Server Error",
   131      "detail": "Error fetching ApiEndpoints"
   132  }`),
   133  			expectedPath: "/appsec/v1/configs/43253/versions/15/api-endpoints",
   134  			withError: &Error{
   135  				Type:       "internal_error",
   136  				Title:      "Internal Server Error",
   137  				Detail:     "Error fetching ApiEndpoints",
   138  				StatusCode: http.StatusInternalServerError,
   139  			},
   140  		},
   141  	}
   142  
   143  	for name, test := range tests {
   144  		t.Run(name, func(t *testing.T) {
   145  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   146  				assert.Equal(t, test.expectedPath, r.URL.String())
   147  				assert.Equal(t, http.MethodGet, r.Method)
   148  				w.WriteHeader(test.responseStatus)
   149  				_, err := w.Write([]byte(test.responseBody))
   150  				assert.NoError(t, err)
   151  			}))
   152  			client := mockAPIClient(t, mockServer)
   153  			result, err := client.GetApiEndpoints(context.Background(), test.params)
   154  			if test.withError != nil {
   155  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   156  				return
   157  			}
   158  			require.NoError(t, err)
   159  			assert.Equal(t, test.expectedResponse, result)
   160  		})
   161  	}
   162  }