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