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

     1  package networklists
     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 TestApsec_ListNetworkListSubscription(t *testing.T) {
    17  
    18  	result := GetNetworkListSubscriptionResponse{}
    19  
    20  	respData := compactJSON(loadFixtureBytes("testdata/TestNetworkListSubscription/NetworkListSubscription.json"))
    21  	json.Unmarshal([]byte(respData), &result)
    22  
    23  	tests := map[string]struct {
    24  		params           GetNetworkListSubscriptionRequest
    25  		responseStatus   int
    26  		responseBody     string
    27  		expectedPath     string
    28  		expectedResponse *GetNetworkListSubscriptionResponse
    29  		withError        error
    30  		headers          http.Header
    31  	}{
    32  		"200 OK": {
    33  			params: GetNetworkListSubscriptionRequest{},
    34  			headers: http.Header{
    35  				"Content-Type": []string{"application/json"},
    36  			},
    37  			responseStatus:   http.StatusOK,
    38  			responseBody:     respData,
    39  			expectedPath:     "/network-list/v2/notifications/subscriptions",
    40  			expectedResponse: &result,
    41  		},
    42  		"500 internal server error": {
    43  			params:         GetNetworkListSubscriptionRequest{},
    44  			headers:        http.Header{},
    45  			responseStatus: http.StatusInternalServerError,
    46  			responseBody: `
    47  {
    48      "type": "internal_error",
    49      "title": "Internal Server Error",
    50      "detail": "Error fetching subscriptions",
    51      "status": 500
    52  }`,
    53  			expectedPath: "/network-list/v2/notifications/subscriptions",
    54  			withError: &Error{
    55  				Type:       "internal_error",
    56  				Title:      "Internal Server Error",
    57  				Detail:     "Error fetching subscriptions",
    58  				StatusCode: http.StatusInternalServerError,
    59  			},
    60  		},
    61  	}
    62  
    63  	for name, test := range tests {
    64  		t.Run(name, func(t *testing.T) {
    65  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    66  				assert.Equal(t, test.expectedPath, r.URL.String())
    67  				assert.Equal(t, http.MethodGet, r.Method)
    68  				w.WriteHeader(test.responseStatus)
    69  				_, err := w.Write([]byte(test.responseBody))
    70  				assert.NoError(t, err)
    71  			}))
    72  			client := mockAPIClient(t, mockServer)
    73  			result, err := client.GetNetworkListSubscription(
    74  				session.ContextWithOptions(
    75  					context.Background(),
    76  					session.WithContextHeaders(test.headers),
    77  				),
    78  				test.params)
    79  			if test.withError != nil {
    80  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
    81  				return
    82  			}
    83  			require.NoError(t, err)
    84  			assert.Equal(t, test.expectedResponse, result)
    85  		})
    86  	}
    87  }
    88  
    89  // Test NetworkListSubscription
    90  func TestAppSec_GetNetworkListSubscription(t *testing.T) {
    91  
    92  	result := GetNetworkListSubscriptionResponse{}
    93  
    94  	respData := compactJSON(loadFixtureBytes("testdata/TestNetworkListSubscription/NetworkListSubscription.json"))
    95  	json.Unmarshal([]byte(respData), &result)
    96  
    97  	tests := map[string]struct {
    98  		params           GetNetworkListSubscriptionRequest
    99  		responseStatus   int
   100  		responseBody     string
   101  		expectedPath     string
   102  		expectedResponse *GetNetworkListSubscriptionResponse
   103  		withError        error
   104  	}{
   105  		"200 OK": {
   106  			params:           GetNetworkListSubscriptionRequest{},
   107  			responseStatus:   http.StatusOK,
   108  			responseBody:     respData,
   109  			expectedPath:     "/network-list/v2/notifications/subscriptions",
   110  			expectedResponse: &result,
   111  		},
   112  		"500 internal server error": {
   113  			params:         GetNetworkListSubscriptionRequest{},
   114  			responseStatus: http.StatusInternalServerError,
   115  			responseBody: `
   116  {
   117      "type": "internal_error",
   118      "title": "Internal Server Error",
   119      "detail": "Error fetching subscriptions"
   120  }`,
   121  			expectedPath: "/network-list/v2/notifications/subscriptions",
   122  			withError: &Error{
   123  				Type:       "internal_error",
   124  				Title:      "Internal Server Error",
   125  				Detail:     "Error fetching subscriptions",
   126  				StatusCode: http.StatusInternalServerError,
   127  			},
   128  		},
   129  	}
   130  
   131  	for name, test := range tests {
   132  		t.Run(name, func(t *testing.T) {
   133  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   134  				assert.Equal(t, test.expectedPath, r.URL.String())
   135  				assert.Equal(t, http.MethodGet, r.Method)
   136  				w.WriteHeader(test.responseStatus)
   137  				_, err := w.Write([]byte(test.responseBody))
   138  				assert.NoError(t, err)
   139  			}))
   140  			client := mockAPIClient(t, mockServer)
   141  			result, err := client.GetNetworkListSubscription(context.Background(), test.params)
   142  			if test.withError != nil {
   143  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   144  				return
   145  			}
   146  			require.NoError(t, err)
   147  			assert.Equal(t, test.expectedResponse, result)
   148  		})
   149  	}
   150  }
   151  
   152  // Test Update NetworkListSubscription.
   153  func TestAppSec_UpdateNetworkListSubscription(t *testing.T) {
   154  	result := UpdateNetworkListSubscriptionResponse{}
   155  
   156  	respData := compactJSON(loadFixtureBytes("testdata/TestNetworkListSubscription/NetworkListSubscription.json"))
   157  	json.Unmarshal([]byte(respData), &result)
   158  
   159  	req := UpdateNetworkListSubscriptionRequest{}
   160  
   161  	reqData := compactJSON(loadFixtureBytes("testdata/TestNetworkListSubscription/NetworkListSubscription.json"))
   162  	json.Unmarshal([]byte(reqData), &req)
   163  
   164  	tests := map[string]struct {
   165  		params           UpdateNetworkListSubscriptionRequest
   166  		responseStatus   int
   167  		responseBody     string
   168  		expectedPath     string
   169  		expectedResponse *UpdateNetworkListSubscriptionResponse
   170  		withError        error
   171  		headers          http.Header
   172  	}{
   173  		"200 Success": {
   174  			params: UpdateNetworkListSubscriptionRequest{},
   175  			headers: http.Header{
   176  				"Content-Type": []string{"application/json;charset=UTF-8"},
   177  			},
   178  			responseStatus:   http.StatusCreated,
   179  			responseBody:     respData,
   180  			expectedResponse: &result,
   181  			expectedPath:     "/network-list/v2/notifications/subscribe",
   182  		},
   183  		"500 internal server error": {
   184  			params:         UpdateNetworkListSubscriptionRequest{},
   185  			responseStatus: http.StatusInternalServerError,
   186  			responseBody: `
   187  {
   188      "type": "internal_error",
   189      "title": "Internal Server Error",
   190      "detail": "Error creating subscription"
   191  }`,
   192  			expectedPath: "/network-list/v2/notifications/subscribe",
   193  			withError: &Error{
   194  				Type:       "internal_error",
   195  				Title:      "Internal Server Error",
   196  				Detail:     "Error creating subscription",
   197  				StatusCode: http.StatusInternalServerError,
   198  			},
   199  		},
   200  	}
   201  
   202  	for name, test := range tests {
   203  		t.Run(name, func(t *testing.T) {
   204  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   205  				assert.Equal(t, http.MethodPost, r.Method)
   206  				assert.Equal(t, test.expectedPath, r.URL.String())
   207  				w.WriteHeader(test.responseStatus)
   208  				if len(test.responseBody) > 0 {
   209  					_, err := w.Write([]byte(test.responseBody))
   210  					assert.NoError(t, err)
   211  				}
   212  			}))
   213  			client := mockAPIClient(t, mockServer)
   214  			result, err := client.UpdateNetworkListSubscription(
   215  				session.ContextWithOptions(
   216  					context.Background(),
   217  					session.WithContextHeaders(test.headers)), test.params)
   218  			if test.withError != nil {
   219  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   220  				return
   221  			}
   222  			require.NoError(t, err)
   223  			assert.Equal(t, test.expectedResponse, result)
   224  		})
   225  	}
   226  }