github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/apigw/v2/responses/testing/fixtures.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/huaweicloud/golangsdk/openstack/apigw/v2/responses"
     9  	th "github.com/huaweicloud/golangsdk/testhelper"
    10  	"github.com/huaweicloud/golangsdk/testhelper/client"
    11  )
    12  
    13  const (
    14  	expectedCreateResponse = `
    15  {
    16  	"create_time": "2021-06-24T09:33:10.562277766+08:00",
    17  	"default": false,
    18  	"id": "baabc69fdb8f4c458637666c0441e9a4",
    19  	"name": "terraform-test",
    20  	"responses": {
    21  		"ACCESS_DENIED": {
    22  			"body": "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
    23  			"default": false,
    24  			"status": 402
    25  		},
    26  		"AUTHORIZER_CONF_FAILURE": {
    27  			"body": "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
    28  			"default": true,
    29  			"status": 500
    30  		},
    31  		"AUTHORIZER_FAILURE": {
    32  			"body": "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
    33  			"default": true,
    34  			"status": 500
    35  		},
    36  		"AUTHORIZER_IDENTITIES_FAILURE": {
    37  			"body": "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
    38  			"default": true,
    39  			"status": 401
    40  		},
    41  		"AUTH_FAILURE": {
    42  			"body": "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
    43  			"default": true,
    44  			"status": 401
    45  		},
    46  		"AUTH_HEADER_MISSING": {
    47  			"body": "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
    48  			"default": true,
    49  			"status": 401
    50  		},
    51  		"BACKEND_TIMEOUT": {
    52  			"body": "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
    53  			"default": true,
    54  			"status": 504
    55  		},
    56  		"BACKEND_UNAVAILABLE": {
    57  			"body": "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
    58  			"default": true,
    59  			"status": 502
    60  		},
    61  		"DEFAULT_4XX": {
    62  			"body": "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
    63  			"default": true
    64  		},
    65  		"DEFAULT_5XX": {
    66  			"body": "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
    67  			"default": true
    68  		},
    69  		"NOT_FOUND": {
    70  			"body": "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
    71  			"default": true,
    72  			"status": 404
    73  		},
    74  		"REQUEST_PARAMETERS_FAILURE": {
    75  			"body": "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
    76  			"default": true,
    77  			"status": 400
    78  		},
    79  		"THROTTLED": {
    80  			"body": "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
    81  			"default": true,
    82  			"status": 429
    83  		},
    84  		"UNAUTHORIZED": {
    85  			"body": "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
    86  			"default": true,
    87  			"status": 401
    88  		}
    89  	},
    90  	"update_time": "2021-06-24T09:33:10.562277766+08:00"
    91  }`
    92  	expectedListResponse = `
    93  {
    94  	"responses": [
    95          {
    96              "id": "baabc69fdb8f4c458637666c0441e9a4",
    97              "name": "terraform-test",
    98              "default": false,
    99              "create_time": "2021-06-24T01:33:10Z",
   100              "update_time": "2021-06-24T01:33:10Z"
   101          },
   102          {
   103              "id": "5623b9b3c2154f6ab1a7c0cf5c7c6278",
   104              "name": "default",
   105              "default": true,
   106              "create_time": "2021-06-23T08:29:23Z",
   107              "update_time": "2021-06-23T08:29:23Z"
   108          }
   109      ]
   110  }`
   111  	expectedGetSpecResponse = `
   112  {
   113  	"ACCESS_DENIED": {
   114          "body": "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
   115          "default": false,
   116          "status": 405
   117      }
   118  }`
   119  )
   120  
   121  var (
   122  	createOpts = &responses.ResponseOpts{
   123  		Name: "terraform-test",
   124  		Responses: map[string]responses.ResponseInfo{
   125  			"ACCESS_DENIED": {
   126  				Body:   "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
   127  				Status: 402,
   128  			},
   129  		},
   130  		InstanceId: "9b76174b785342078e557f23c01d5e41",
   131  		GroupId:    "d060ade0560a4c01b89bf954ad2e9d6e",
   132  	}
   133  
   134  	expectedCreateResponseData = &responses.Response{
   135  		CreateTime: "2021-06-24T09:33:10.562277766+08:00",
   136  		UpdateTime: "2021-06-24T09:33:10.562277766+08:00",
   137  		IsDefault:  false,
   138  		Id:         "baabc69fdb8f4c458637666c0441e9a4",
   139  		Name:       "terraform-test",
   140  		Responses: map[string]responses.ResponseInfo{
   141  			"ACCESS_DENIED": {
   142  				Body:      "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
   143  				IsDefault: false,
   144  				Status:    402,
   145  			},
   146  			"AUTHORIZER_CONF_FAILURE": {
   147  				Body:      "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
   148  				IsDefault: true,
   149  				Status:    500,
   150  			},
   151  			"AUTHORIZER_FAILURE": {
   152  				Body:      "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
   153  				IsDefault: true,
   154  				Status:    500,
   155  			},
   156  			"AUTHORIZER_IDENTITIES_FAILURE": {
   157  				Body:      "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
   158  				IsDefault: true,
   159  				Status:    401,
   160  			},
   161  			"AUTH_FAILURE": {
   162  				Body:      "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
   163  				IsDefault: true,
   164  				Status:    401,
   165  			},
   166  			"AUTH_HEADER_MISSING": {
   167  				Body:      "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
   168  				IsDefault: true,
   169  				Status:    401,
   170  			},
   171  			"BACKEND_TIMEOUT": {
   172  				Body:      "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
   173  				IsDefault: true,
   174  				Status:    504,
   175  			},
   176  			"BACKEND_UNAVAILABLE": {
   177  				Body:      "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
   178  				IsDefault: true,
   179  				Status:    502,
   180  			},
   181  			"DEFAULT_4XX": {
   182  				Body:      "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
   183  				IsDefault: true,
   184  			},
   185  			"DEFAULT_5XX": {
   186  				Body:      "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
   187  				IsDefault: true,
   188  			},
   189  			"NOT_FOUND": {
   190  				Body:      "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
   191  				IsDefault: true,
   192  				Status:    404,
   193  			},
   194  			"REQUEST_PARAMETERS_FAILURE": {
   195  				Body:      "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
   196  				IsDefault: true,
   197  				Status:    400,
   198  			},
   199  			"THROTTLED": {
   200  				Body:      "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
   201  				IsDefault: true,
   202  				Status:    429,
   203  			},
   204  			"UNAUTHORIZED": {
   205  				Body:      "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
   206  				IsDefault: true,
   207  				Status:    401,
   208  			},
   209  		},
   210  	}
   211  
   212  	listOpts = &responses.ListOpts{
   213  		InstanceId: "9b76174b785342078e557f23c01d5e41",
   214  		GroupId:    "d060ade0560a4c01b89bf954ad2e9d6e",
   215  	}
   216  
   217  	expectedListResponseData = []responses.Response{
   218  		{
   219  			Id:         "baabc69fdb8f4c458637666c0441e9a4",
   220  			Name:       "terraform-test",
   221  			IsDefault:  false,
   222  			CreateTime: "2021-06-24T01:33:10Z",
   223  			UpdateTime: "2021-06-24T01:33:10Z",
   224  		},
   225  		{
   226  			Id:         "5623b9b3c2154f6ab1a7c0cf5c7c6278",
   227  			Name:       "default",
   228  			IsDefault:  true,
   229  			CreateTime: "2021-06-23T08:29:23Z",
   230  			UpdateTime: "2021-06-23T08:29:23Z",
   231  		},
   232  	}
   233  
   234  	updateOpts = &responses.ResponseOpts{
   235  		Name:       "terraform-test",
   236  		InstanceId: "9b76174b785342078e557f23c01d5e41",
   237  		GroupId:    "d060ade0560a4c01b89bf954ad2e9d6e",
   238  	}
   239  	specRespOpts = &responses.SpecRespOpts{
   240  		InstanceId: "9b76174b785342078e557f23c01d5e41",
   241  		GroupId:    "d060ade0560a4c01b89bf954ad2e9d6e",
   242  		RespId:     "baabc69fdb8f4c458637666c0441e9a4",
   243  	}
   244  
   245  	responseInfoOpts = &responses.ResponseInfo{
   246  		Body:   "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
   247  		Status: 405,
   248  	}
   249  
   250  	expectedGetSpecResponseData = &responses.ResponseInfo{
   251  		Body:      "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
   252  		Status:    405,
   253  		IsDefault: false,
   254  	}
   255  
   256  	updateSpecRespOpts = responses.ResponseInfo{
   257  		Body:   "{\"error_code\":\"$context.error.code\",\"error_msg\":\"$context.error.message\",\"request_id\":\"$context.requestId\"}",
   258  		Status: 405,
   259  	}
   260  )
   261  
   262  func handleV2ResponsesCreate(t *testing.T) {
   263  	th.Mux.HandleFunc("/instances/9b76174b785342078e557f23c01d5e41/api-groups/d060ade0560a4c01b89bf954ad2e9d6e"+
   264  		"/gateway-responses", func(w http.ResponseWriter, r *http.Request) {
   265  		th.TestMethod(t, r, "POST")
   266  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   267  		w.Header().Add("Content-Type", "application/json")
   268  		w.WriteHeader(http.StatusAccepted)
   269  		_, _ = fmt.Fprint(w, expectedCreateResponse)
   270  	})
   271  }
   272  
   273  func handleV2ResponsesGet(t *testing.T) {
   274  	th.Mux.HandleFunc("/instances/9b76174b785342078e557f23c01d5e41/api-groups/d060ade0560a4c01b89bf954ad2e9d6e"+
   275  		"/gateway-responses/baabc69fdb8f4c458637666c0441e9a4", func(w http.ResponseWriter, r *http.Request) {
   276  		th.TestMethod(t, r, "GET")
   277  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   278  		w.Header().Add("Content-Type", "application/json")
   279  		w.WriteHeader(http.StatusOK)
   280  		_, _ = fmt.Fprint(w, expectedCreateResponse)
   281  	})
   282  }
   283  
   284  func handleV2ResponsesList(t *testing.T) {
   285  	th.Mux.HandleFunc("/instances/9b76174b785342078e557f23c01d5e41/api-groups/d060ade0560a4c01b89bf954ad2e9d6e"+
   286  		"/gateway-responses", func(w http.ResponseWriter, r *http.Request) {
   287  		th.TestMethod(t, r, "GET")
   288  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   289  		w.Header().Add("Content-Type", "application/json")
   290  		w.WriteHeader(http.StatusOK)
   291  		_, _ = fmt.Fprint(w, expectedListResponse)
   292  	})
   293  }
   294  
   295  func handleV2ResponsesUpdate(t *testing.T) {
   296  	th.Mux.HandleFunc("/instances/9b76174b785342078e557f23c01d5e41/api-groups/d060ade0560a4c01b89bf954ad2e9d6e"+
   297  		"/gateway-responses/baabc69fdb8f4c458637666c0441e9a4", func(w http.ResponseWriter, r *http.Request) {
   298  		th.TestMethod(t, r, "PUT")
   299  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   300  		w.Header().Add("Content-Type", "application/json")
   301  		w.WriteHeader(http.StatusOK)
   302  		_, _ = fmt.Fprint(w, expectedCreateResponse)
   303  	})
   304  }
   305  
   306  func handleV2ResponsesDelete(t *testing.T) {
   307  	th.Mux.HandleFunc("/instances/9b76174b785342078e557f23c01d5e41/api-groups/d060ade0560a4c01b89bf954ad2e9d6e"+
   308  		"/gateway-responses/baabc69fdb8f4c458637666c0441e9a4", func(w http.ResponseWriter, r *http.Request) {
   309  		th.TestMethod(t, r, "DELETE")
   310  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   311  		w.Header().Add("Content-Type", "application/json")
   312  		w.WriteHeader(http.StatusNoContent)
   313  	})
   314  }
   315  
   316  func handleV2SpecResponseGet(t *testing.T) {
   317  	th.Mux.HandleFunc("/instances/9b76174b785342078e557f23c01d5e41/api-groups/d060ade0560a4c01b89bf954ad2e9d6e"+
   318  		"/gateway-responses/baabc69fdb8f4c458637666c0441e9a4/ACCESS_DENIED", func(w http.ResponseWriter, r *http.Request) {
   319  		th.TestMethod(t, r, "GET")
   320  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   321  		w.Header().Add("Content-Type", "application/json")
   322  		w.WriteHeader(http.StatusOK)
   323  		_, _ = fmt.Fprint(w, expectedGetSpecResponse)
   324  	})
   325  }
   326  
   327  func handleV2SpecResponseUpdate(t *testing.T) {
   328  	th.Mux.HandleFunc("/instances/9b76174b785342078e557f23c01d5e41/api-groups/d060ade0560a4c01b89bf954ad2e9d6e"+
   329  		"/gateway-responses/baabc69fdb8f4c458637666c0441e9a4/ACCESS_DENIED", func(w http.ResponseWriter, r *http.Request) {
   330  		th.TestMethod(t, r, "PUT")
   331  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   332  		w.Header().Add("Content-Type", "application/json")
   333  		w.WriteHeader(http.StatusOK)
   334  		_, _ = fmt.Fprint(w, expectedGetSpecResponse)
   335  	})
   336  }
   337  
   338  func handleV2SpecResponseDelete(t *testing.T) {
   339  	th.Mux.HandleFunc("/instances/9b76174b785342078e557f23c01d5e41/api-groups/d060ade0560a4c01b89bf954ad2e9d6e"+
   340  		"/gateway-responses/baabc69fdb8f4c458637666c0441e9a4/ACCESS_DENIED", func(w http.ResponseWriter, r *http.Request) {
   341  		th.TestMethod(t, r, "DELETE")
   342  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   343  		w.Header().Add("Content-Type", "application/json")
   344  		w.WriteHeader(http.StatusNoContent)
   345  	})
   346  }