github.com/akamai/AkamaiOPEN-edgegrid-golang/v2@v2.17.0/pkg/appsec/api_hostname_coverage_overlapping_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_ListApiHostnameCoverageOverlapping(t *testing.T) {
    17  
    18  	result := GetApiHostnameCoverageOverlappingResponse{}
    19  
    20  	respData := compactJSON(loadFixtureBytes("testdata/TestApiHostnameCoverageOverlapping/ApiHostnameCoverageOverlapping.json"))
    21  	json.Unmarshal([]byte(respData), &result)
    22  
    23  	tests := map[string]struct {
    24  		params           GetApiHostnameCoverageOverlappingRequest
    25  		responseStatus   int
    26  		responseBody     string
    27  		expectedPath     string
    28  		expectedResponse *GetApiHostnameCoverageOverlappingResponse
    29  		withError        error
    30  		headers          http.Header
    31  	}{
    32  		"200 OK": {
    33  			params: GetApiHostnameCoverageOverlappingRequest{
    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/hostname-coverage/overlapping?hostname=",
    43  			expectedResponse: &result,
    44  		},
    45  		"500 internal server error": {
    46  			params: GetApiHostnameCoverageOverlappingRequest{
    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 ApiHostnameCoverageOverlapping",
    57      "status": 500
    58  }`,
    59  			expectedPath: "/appsec/v1/configs/43253/versions/15/hostname-coverage/overlapping?hostname=",
    60  			withError: &Error{
    61  				Type:       "internal_error",
    62  				Title:      "Internal Server Error",
    63  				Detail:     "Error fetching ApiHostnameCoverageOverlapping",
    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.GetApiHostnameCoverageOverlapping(
    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 ApiHostnameCoverageOverlapping
    96  func TestAppSec_GetApiHostnameCoverageOverlapping(t *testing.T) {
    97  
    98  	result := GetApiHostnameCoverageOverlappingResponse{}
    99  
   100  	respData := compactJSON(loadFixtureBytes("testdata/TestApiHostnameCoverageOverlapping/ApiHostnameCoverageOverlapping.json"))
   101  	json.Unmarshal([]byte(respData), &result)
   102  
   103  	tests := map[string]struct {
   104  		params           GetApiHostnameCoverageOverlappingRequest
   105  		responseStatus   int
   106  		responseBody     string
   107  		expectedPath     string
   108  		expectedResponse *GetApiHostnameCoverageOverlappingResponse
   109  		withError        error
   110  	}{
   111  		"200 OK": {
   112  			params: GetApiHostnameCoverageOverlappingRequest{
   113  				ConfigID: 43253,
   114  				Version:  15,
   115  				Hostname: "www.example.com",
   116  			},
   117  			responseStatus:   http.StatusOK,
   118  			responseBody:     respData,
   119  			expectedPath:     "/appsec/v1/configs/43253/versions/15/hostname-coverage/overlapping?hostname=www.example.com",
   120  			expectedResponse: &result,
   121  		},
   122  		"500 internal server error": {
   123  			params: GetApiHostnameCoverageOverlappingRequest{
   124  				ConfigID: 43253,
   125  				Version:  15,
   126  				Hostname: "www.example.com",
   127  			},
   128  			responseStatus: http.StatusInternalServerError,
   129  			responseBody: (`
   130  {
   131      "type": "internal_error",
   132      "title": "Internal Server Error",
   133      "detail": "Error fetching ApiHostnameCoverageOverlapping"
   134  }`),
   135  			expectedPath: "/appsec/v1/configs/43253/versions/15/hostname-coverage/overlapping?hostname=www.example.com",
   136  			withError: &Error{
   137  				Type:       "internal_error",
   138  				Title:      "Internal Server Error",
   139  				Detail:     "Error fetching ApiHostnameCoverageOverlapping",
   140  				StatusCode: http.StatusInternalServerError,
   141  			},
   142  		},
   143  	}
   144  
   145  	for name, test := range tests {
   146  		t.Run(name, func(t *testing.T) {
   147  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   148  				assert.Equal(t, test.expectedPath, r.URL.String())
   149  				assert.Equal(t, http.MethodGet, r.Method)
   150  				w.WriteHeader(test.responseStatus)
   151  				_, err := w.Write([]byte(test.responseBody))
   152  				assert.NoError(t, err)
   153  			}))
   154  			client := mockAPIClient(t, mockServer)
   155  			result, err := client.GetApiHostnameCoverageOverlapping(context.Background(), test.params)
   156  			if test.withError != nil {
   157  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   158  				return
   159  			}
   160  			require.NoError(t, err)
   161  			assert.Equal(t, test.expectedResponse, result)
   162  		})
   163  	}
   164  }