github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/loadbalancer/v2/l7policies/testing/fixtures_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/loadbalancer/v2/l7policies"
     9  	th "github.com/vnpaycloud-console/gophercloud/v2/testhelper"
    10  	"github.com/vnpaycloud-console/gophercloud/v2/testhelper/client"
    11  )
    12  
    13  // SingleL7PolicyBody is the canned body of a Get request on an existing l7policy.
    14  const SingleL7PolicyBody = `
    15  {
    16  	"l7policy": {
    17  		"listener_id": "023f2e34-7806-443b-bfae-16c324569a3d",
    18  		"description": "",
    19  		"admin_state_up": true,
    20  		"redirect_pool_id": null,
    21  		"redirect_url": "http://www.example.com",
    22  		"action": "REDIRECT_TO_URL",
    23  		"position": 1,
    24  		"project_id": "e3cd678b11784734bc366148aa37580e",
    25  		"id": "8a1412f0-4c32-4257-8b07-af4770b604fd",
    26  		"name": "redirect-example.com",
    27  		"rules": []
    28  	}
    29  }
    30  `
    31  
    32  var (
    33  	L7PolicyToURL = l7policies.L7Policy{
    34  		ID:             "8a1412f0-4c32-4257-8b07-af4770b604fd",
    35  		Name:           "redirect-example.com",
    36  		ListenerID:     "023f2e34-7806-443b-bfae-16c324569a3d",
    37  		Action:         "REDIRECT_TO_URL",
    38  		Position:       1,
    39  		Description:    "",
    40  		ProjectID:      "e3cd678b11784734bc366148aa37580e",
    41  		RedirectPoolID: "",
    42  		RedirectURL:    "http://www.example.com",
    43  		AdminStateUp:   true,
    44  		Rules:          []l7policies.Rule{},
    45  	}
    46  	L7PolicyToPool = l7policies.L7Policy{
    47  		ID:             "964f4ba4-f6cd-405c-bebd-639460af7231",
    48  		Name:           "redirect-pool",
    49  		ListenerID:     "be3138a3-5cf7-4513-a4c2-bb137e668bab",
    50  		Action:         "REDIRECT_TO_POOL",
    51  		Position:       1,
    52  		Description:    "",
    53  		ProjectID:      "c1f7910086964990847dc6c8b128f63c",
    54  		RedirectPoolID: "bac433c6-5bea-4311-80da-bd1cd90fbd25",
    55  		RedirectURL:    "",
    56  		AdminStateUp:   true,
    57  		Rules:          []l7policies.Rule{},
    58  	}
    59  	L7PolicyUpdated = l7policies.L7Policy{
    60  		ID:             "8a1412f0-4c32-4257-8b07-af4770b604fd",
    61  		Name:           "NewL7PolicyName",
    62  		ListenerID:     "023f2e34-7806-443b-bfae-16c324569a3d",
    63  		Action:         "REDIRECT_TO_URL",
    64  		Position:       1,
    65  		Description:    "Redirect requests to example.com",
    66  		ProjectID:      "e3cd678b11784734bc366148aa37580e",
    67  		RedirectPoolID: "",
    68  		RedirectURL:    "http://www.new-example.com",
    69  		AdminStateUp:   true,
    70  		Rules:          []l7policies.Rule{},
    71  	}
    72  	L7PolicyNullRedirectURLUpdated = l7policies.L7Policy{
    73  		ID:             "8a1412f0-4c32-4257-8b07-af4770b604fd",
    74  		Name:           "NewL7PolicyName",
    75  		ListenerID:     "023f2e34-7806-443b-bfae-16c324569a3d",
    76  		Action:         "REDIRECT_TO_URL",
    77  		Position:       1,
    78  		Description:    "Redirect requests to example.com",
    79  		ProjectID:      "e3cd678b11784734bc366148aa37580e",
    80  		RedirectPoolID: "",
    81  		RedirectURL:    "",
    82  		AdminStateUp:   true,
    83  		Rules:          []l7policies.Rule{},
    84  	}
    85  	RulePath = l7policies.Rule{
    86  		ID:           "16621dbb-a736-4888-a57a-3ecd53df784c",
    87  		RuleType:     "PATH",
    88  		CompareType:  "REGEX",
    89  		Value:        "/images*",
    90  		ProjectID:    "e3cd678b11784734bc366148aa37580e",
    91  		Key:          "",
    92  		Invert:       true,
    93  		AdminStateUp: true,
    94  	}
    95  	RuleHostName = l7policies.Rule{
    96  		ID:           "d24521a0-df84-4468-861a-a531af116d1e",
    97  		RuleType:     "HOST_NAME",
    98  		CompareType:  "EQUAL_TO",
    99  		Value:        "www.example.com",
   100  		ProjectID:    "e3cd678b11784734bc366148aa37580e",
   101  		Key:          "",
   102  		Invert:       false,
   103  		AdminStateUp: true,
   104  	}
   105  	RuleUpdated = l7policies.Rule{
   106  		ID:           "16621dbb-a736-4888-a57a-3ecd53df784c",
   107  		RuleType:     "PATH",
   108  		CompareType:  "REGEX",
   109  		Value:        "/images/special*",
   110  		ProjectID:    "e3cd678b11784734bc366148aa37580e",
   111  		Key:          "",
   112  		Invert:       false,
   113  		AdminStateUp: true,
   114  	}
   115  )
   116  
   117  // HandleL7PolicyCreationSuccessfully sets up the test server to respond to a l7policy creation request
   118  // with a given response.
   119  func HandleL7PolicyCreationSuccessfully(t *testing.T, response string) {
   120  	th.Mux.HandleFunc("/v2.0/lbaas/l7policies", func(w http.ResponseWriter, r *http.Request) {
   121  		th.TestMethod(t, r, "POST")
   122  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   123  		th.TestJSONRequest(t, r, `{
   124  			"l7policy": {
   125  				"listener_id": "023f2e34-7806-443b-bfae-16c324569a3d",
   126  				"redirect_url": "http://www.example.com",
   127  				"name": "redirect-example.com",
   128  				"action": "REDIRECT_TO_URL"
   129  			}
   130  		}`)
   131  
   132  		w.WriteHeader(http.StatusAccepted)
   133  		w.Header().Add("Content-Type", "application/json")
   134  		fmt.Fprint(w, response)
   135  	})
   136  }
   137  
   138  // L7PoliciesListBody contains the canned body of a l7policy list response.
   139  const L7PoliciesListBody = `
   140  {
   141      "l7policies": [
   142          {
   143              "redirect_pool_id": null,
   144              "description": "",
   145              "admin_state_up": true,
   146              "rules": [],
   147              "project_id": "e3cd678b11784734bc366148aa37580e",
   148              "listener_id": "023f2e34-7806-443b-bfae-16c324569a3d",
   149              "redirect_url": "http://www.example.com",
   150              "action": "REDIRECT_TO_URL",
   151              "position": 1,
   152              "id": "8a1412f0-4c32-4257-8b07-af4770b604fd",
   153              "name": "redirect-example.com"
   154          },
   155          {
   156              "redirect_pool_id": "bac433c6-5bea-4311-80da-bd1cd90fbd25",
   157              "description": "",
   158              "admin_state_up": true,
   159              "rules": [],
   160              "project_id": "c1f7910086964990847dc6c8b128f63c",
   161              "listener_id": "be3138a3-5cf7-4513-a4c2-bb137e668bab",
   162              "action": "REDIRECT_TO_POOL",
   163              "position": 1,
   164              "id": "964f4ba4-f6cd-405c-bebd-639460af7231",
   165              "name": "redirect-pool"
   166          }
   167      ]
   168  }
   169  `
   170  
   171  // PostUpdateL7PolicyBody is the canned response body of a Update request on an existing l7policy.
   172  const PostUpdateL7PolicyBody = `
   173  {
   174  	"l7policy": {
   175  		"listener_id": "023f2e34-7806-443b-bfae-16c324569a3d",
   176  		"description": "Redirect requests to example.com",
   177  		"admin_state_up": true,
   178  		"redirect_url": "http://www.new-example.com",
   179  		"action": "REDIRECT_TO_URL",
   180  		"position": 1,
   181  		"project_id": "e3cd678b11784734bc366148aa37580e",
   182  		"id": "8a1412f0-4c32-4257-8b07-af4770b604fd",
   183  		"name": "NewL7PolicyName",
   184  		"rules": []
   185  	}
   186  }
   187  `
   188  
   189  // PostUpdateL7PolicyNullRedirectURLBody is the canned response body of a Update request
   190  // on an existing l7policy with a null redirect_url .
   191  const PostUpdateL7PolicyNullRedirectURLBody = `
   192  {
   193  	"l7policy": {
   194  		"listener_id": "023f2e34-7806-443b-bfae-16c324569a3d",
   195  		"description": "Redirect requests to example.com",
   196  		"admin_state_up": true,
   197  		"redirect_url": null,
   198  		"action": "REDIRECT_TO_URL",
   199  		"position": 1,
   200  		"project_id": "e3cd678b11784734bc366148aa37580e",
   201  		"id": "8a1412f0-4c32-4257-8b07-af4770b604fd",
   202  		"name": "NewL7PolicyName",
   203  		"rules": []
   204  	}
   205  }
   206  `
   207  
   208  // HandleL7PolicyListSuccessfully sets up the test server to respond to a l7policy List request.
   209  func HandleL7PolicyListSuccessfully(t *testing.T) {
   210  	th.Mux.HandleFunc("/v2.0/lbaas/l7policies", func(w http.ResponseWriter, r *http.Request) {
   211  		th.TestMethod(t, r, "GET")
   212  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   213  
   214  		w.Header().Add("Content-Type", "application/json")
   215  		if err := r.ParseForm(); err != nil {
   216  			t.Errorf("Failed to parse request form %v", err)
   217  		}
   218  		marker := r.Form.Get("marker")
   219  		switch marker {
   220  		case "":
   221  			fmt.Fprint(w, L7PoliciesListBody)
   222  		case "45e08a3e-a78f-4b40-a229-1e7e23eee1ab":
   223  			fmt.Fprint(w, `{ "l7policies": [] }`)
   224  		default:
   225  			t.Fatalf("/v2.0/lbaas/l7policies invoked with unexpected marker=[%s]", marker)
   226  		}
   227  	})
   228  }
   229  
   230  // HandleL7PolicyGetSuccessfully sets up the test server to respond to a l7policy Get request.
   231  func HandleL7PolicyGetSuccessfully(t *testing.T) {
   232  	th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd", func(w http.ResponseWriter, r *http.Request) {
   233  		th.TestMethod(t, r, "GET")
   234  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   235  		th.TestHeader(t, r, "Accept", "application/json")
   236  
   237  		fmt.Fprint(w, SingleL7PolicyBody)
   238  	})
   239  }
   240  
   241  // HandleL7PolicyDeletionSuccessfully sets up the test server to respond to a l7policy deletion request.
   242  func HandleL7PolicyDeletionSuccessfully(t *testing.T) {
   243  	th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd", func(w http.ResponseWriter, r *http.Request) {
   244  		th.TestMethod(t, r, "DELETE")
   245  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   246  
   247  		w.WriteHeader(http.StatusNoContent)
   248  	})
   249  }
   250  
   251  // HandleL7PolicyUpdateSuccessfully sets up the test server to respond to a l7policy Update request.
   252  func HandleL7PolicyUpdateSuccessfully(t *testing.T) {
   253  	th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd", func(w http.ResponseWriter, r *http.Request) {
   254  		th.TestMethod(t, r, "PUT")
   255  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   256  		th.TestHeader(t, r, "Accept", "application/json")
   257  		th.TestHeader(t, r, "Content-Type", "application/json")
   258  		th.TestJSONRequest(t, r, `{
   259  			"l7policy": {
   260  				"name": "NewL7PolicyName",
   261  				"action": "REDIRECT_TO_URL",
   262  				"redirect_url": "http://www.new-example.com"
   263  			}
   264  		}`)
   265  
   266  		fmt.Fprint(w, PostUpdateL7PolicyBody)
   267  	})
   268  }
   269  
   270  // HandleL7PolicyUpdateNullRedirectURLSuccessfully sets up the test server to respond to a l7policy Update request.
   271  func HandleL7PolicyUpdateNullRedirectURLSuccessfully(t *testing.T) {
   272  	th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd", func(w http.ResponseWriter, r *http.Request) {
   273  		th.TestMethod(t, r, "PUT")
   274  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   275  		th.TestHeader(t, r, "Accept", "application/json")
   276  		th.TestHeader(t, r, "Content-Type", "application/json")
   277  		th.TestJSONRequest(t, r, `{
   278  			"l7policy": {
   279  				"name": "NewL7PolicyName",
   280  				"redirect_url": null
   281  			}
   282  		}`)
   283  
   284  		fmt.Fprint(w, PostUpdateL7PolicyNullRedirectURLBody)
   285  	})
   286  }
   287  
   288  // SingleRuleBody is the canned body of a Get request on an existing rule.
   289  const SingleRuleBody = `
   290  {
   291  	"rule": {
   292  		"compare_type": "REGEX",
   293  		"invert": true,
   294  		"admin_state_up": true,
   295  		"value": "/images*",
   296  		"key": null,
   297  		"project_id": "e3cd678b11784734bc366148aa37580e",
   298  		"type": "PATH",
   299  		"id": "16621dbb-a736-4888-a57a-3ecd53df784c"
   300  	}
   301  }
   302  `
   303  
   304  // HandleRuleCreationSuccessfully sets up the test server to respond to a rule creation request
   305  // with a given response.
   306  func HandleRuleCreationSuccessfully(t *testing.T, response string) {
   307  	th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd/rules", func(w http.ResponseWriter, r *http.Request) {
   308  		th.TestMethod(t, r, "POST")
   309  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   310  		th.TestJSONRequest(t, r, `{
   311  			"rule": {
   312  				"compare_type": "REGEX",
   313  				"type": "PATH",
   314  				"value": "/images*"
   315  			}
   316  		}`)
   317  
   318  		w.WriteHeader(http.StatusAccepted)
   319  		w.Header().Add("Content-Type", "application/json")
   320  		fmt.Fprint(w, response)
   321  	})
   322  }
   323  
   324  // RulesListBody contains the canned body of a rule list response.
   325  const RulesListBody = `
   326  {
   327  	"rules":[
   328  		{
   329              "compare_type": "REGEX",
   330              "invert": true,
   331              "admin_state_up": true,
   332              "value": "/images*",
   333              "key": null,
   334              "project_id": "e3cd678b11784734bc366148aa37580e",
   335              "type": "PATH",
   336              "id": "16621dbb-a736-4888-a57a-3ecd53df784c"
   337  		},
   338  		{
   339              "compare_type": "EQUAL_TO",
   340              "invert": false,
   341              "admin_state_up": true,
   342              "value": "www.example.com",
   343              "key": null,
   344              "project_id": "e3cd678b11784734bc366148aa37580e",
   345              "type": "HOST_NAME",
   346              "id": "d24521a0-df84-4468-861a-a531af116d1e"
   347  		}
   348  	]
   349  }
   350  `
   351  
   352  // HandleRuleListSuccessfully sets up the test server to respond to a rule List request.
   353  func HandleRuleListSuccessfully(t *testing.T) {
   354  	th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd/rules", func(w http.ResponseWriter, r *http.Request) {
   355  		th.TestMethod(t, r, "GET")
   356  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   357  
   358  		w.Header().Add("Content-Type", "application/json")
   359  		if err := r.ParseForm(); err != nil {
   360  			t.Errorf("Failed to parse request form %v", err)
   361  		}
   362  		marker := r.Form.Get("marker")
   363  		switch marker {
   364  		case "":
   365  			fmt.Fprint(w, RulesListBody)
   366  		case "45e08a3e-a78f-4b40-a229-1e7e23eee1ab":
   367  			fmt.Fprint(w, `{ "rules": [] }`)
   368  		default:
   369  			t.Fatalf("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd/rules invoked with unexpected marker=[%s]", marker)
   370  		}
   371  	})
   372  }
   373  
   374  // HandleRuleGetSuccessfully sets up the test server to respond to a rule Get request.
   375  func HandleRuleGetSuccessfully(t *testing.T) {
   376  	th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd/rules/16621dbb-a736-4888-a57a-3ecd53df784c", func(w http.ResponseWriter, r *http.Request) {
   377  		th.TestMethod(t, r, "GET")
   378  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   379  		th.TestHeader(t, r, "Accept", "application/json")
   380  
   381  		fmt.Fprint(w, SingleRuleBody)
   382  	})
   383  }
   384  
   385  // HandleRuleDeletionSuccessfully sets up the test server to respond to a rule deletion request.
   386  func HandleRuleDeletionSuccessfully(t *testing.T) {
   387  	th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd/rules/16621dbb-a736-4888-a57a-3ecd53df784c", func(w http.ResponseWriter, r *http.Request) {
   388  		th.TestMethod(t, r, "DELETE")
   389  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   390  
   391  		w.WriteHeader(http.StatusNoContent)
   392  	})
   393  }
   394  
   395  // PostUpdateRuleBody is the canned response body of a Update request on an existing rule.
   396  const PostUpdateRuleBody = `
   397  {
   398  	"rule": {
   399  		"compare_type": "REGEX",
   400  		"invert": false,
   401  		"admin_state_up": true,
   402  		"value": "/images/special*",
   403  		"key": null,
   404  		"project_id": "e3cd678b11784734bc366148aa37580e",
   405  		"type": "PATH",
   406  		"id": "16621dbb-a736-4888-a57a-3ecd53df784c"
   407  	}
   408  }
   409  `
   410  
   411  // HandleRuleUpdateSuccessfully sets up the test server to respond to a rule Update request.
   412  func HandleRuleUpdateSuccessfully(t *testing.T) {
   413  	th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd/rules/16621dbb-a736-4888-a57a-3ecd53df784c", func(w http.ResponseWriter, r *http.Request) {
   414  		th.TestMethod(t, r, "PUT")
   415  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   416  		th.TestHeader(t, r, "Accept", "application/json")
   417  		th.TestHeader(t, r, "Content-Type", "application/json")
   418  		th.TestJSONRequest(t, r, `{
   419  			"rule": {
   420  				"compare_type": "REGEX",
   421  				"invert": false,
   422  				"key": null,
   423  				"type": "PATH",
   424  				"value": "/images/special*"
   425  			}
   426  		}`)
   427  
   428  		fmt.Fprint(w, PostUpdateRuleBody)
   429  	})
   430  }