"Fossies" - the Fresh Open Source Software Archive

Member "glance-20.0.1/glance/tests/functional/test_client_redirects.py" (12 Aug 2020, 5049 Bytes) of package /linux/misc/openstack/glance-20.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_client_redirects.py": 20.0.0_vs_20.0.1.

    1 # Copyright 2011 OpenStack Foundation
    2 # All Rights Reserved.
    3 #
    4 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    5 #    not use this file except in compliance with the License. You may obtain
    6 #    a copy of the License at
    7 #
    8 #         http://www.apache.org/licenses/LICENSE-2.0
    9 #
   10 #    Unless required by applicable law or agreed to in writing, software
   11 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   12 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   13 #    License for the specific language governing permissions and limitations
   14 #    under the License.
   15 
   16 """Functional test cases testing glance client redirect-following."""
   17 import eventlet.patcher
   18 from six.moves import http_client as http
   19 import webob.dec
   20 import webob.exc
   21 
   22 from glance.common import client
   23 from glance.common import exception
   24 from glance.common import wsgi
   25 from glance.tests import functional
   26 from glance.tests import utils
   27 
   28 
   29 eventlet.patcher.monkey_patch(socket=True)
   30 
   31 
   32 def RedirectTestApp(name):
   33     class App(object):
   34         """
   35         Test WSGI application which can respond with multiple kinds of HTTP
   36         redirects and is used to verify Glance client redirects.
   37         """
   38         def __init__(self):
   39             """
   40             Initialize app with a name and port.
   41             """
   42             self.name = name
   43 
   44         @webob.dec.wsgify
   45         def __call__(self, request):
   46             """
   47             Handles all requests to the application.
   48             """
   49             base = "http://%s" % request.host
   50             path = request.path_qs
   51 
   52             if path == "/":
   53                 return "root"
   54 
   55             elif path == "/302":
   56                 url = "%s/success" % base
   57                 raise webob.exc.HTTPFound(location=url)
   58 
   59             elif path == "/302?with_qs=yes":
   60                 url = "%s/success?with_qs=yes" % base
   61                 raise webob.exc.HTTPFound(location=url)
   62 
   63             elif path == "/infinite_302":
   64                 raise webob.exc.HTTPFound(location=request.url)
   65 
   66             elif path.startswith("/redirect-to"):
   67                 url = "http://127.0.0.1:%s/success" % path.split("-")[-1]
   68                 raise webob.exc.HTTPFound(location=url)
   69 
   70             elif path == "/success":
   71                 return "success_from_host_%s" % self.name
   72 
   73             elif path == "/success?with_qs=yes":
   74                 return "success_with_qs"
   75 
   76             return "fail"
   77 
   78     return App
   79 
   80 
   81 class TestClientRedirects(functional.FunctionalTest):
   82 
   83     def setUp(self):
   84         super(TestClientRedirects, self).setUp()
   85         self.port_one = utils.get_unused_port()
   86         self.port_two = utils.get_unused_port()
   87         server_one = wsgi.Server()
   88         server_two = wsgi.Server()
   89         self.config(bind_host='127.0.0.1')
   90         self.config(workers=0)
   91         server_one.start(RedirectTestApp("one")(), self.port_one)
   92         server_two.start(RedirectTestApp("two")(), self.port_two)
   93         self.client = client.BaseClient("127.0.0.1", self.port_one)
   94 
   95     def test_get_without_redirect(self):
   96         """
   97         Test GET with no redirect
   98         """
   99         response = self.client.do_request("GET", "/")
  100         self.assertEqual(http.OK, response.status)
  101         self.assertEqual(b"root", response.read())
  102 
  103     def test_get_with_one_redirect(self):
  104         """
  105         Test GET with one 302 FOUND redirect
  106         """
  107         response = self.client.do_request("GET", "/302")
  108         self.assertEqual(http.OK, response.status)
  109         self.assertEqual(b"success_from_host_one", response.read())
  110 
  111     def test_get_with_one_redirect_query_string(self):
  112         """
  113         Test GET with one 302 FOUND redirect w/ a query string
  114         """
  115         response = self.client.do_request("GET", "/302",
  116                                           params={'with_qs': 'yes'})
  117         self.assertEqual(http.OK, response.status)
  118         self.assertEqual(b"success_with_qs", response.read())
  119 
  120     def test_get_with_max_redirects(self):
  121         """
  122         Test we don't redirect forever.
  123         """
  124         self.assertRaises(exception.MaxRedirectsExceeded,
  125                           self.client.do_request,
  126                           "GET",
  127                           "/infinite_302")
  128 
  129     def test_post_redirect(self):
  130         """
  131         Test POST with 302 redirect
  132         """
  133         response = self.client.do_request("POST", "/302")
  134         self.assertEqual(http.OK, response.status)
  135         self.assertEqual(b"success_from_host_one", response.read())
  136 
  137     def test_redirect_to_new_host(self):
  138         """
  139         Test redirect to one host and then another.
  140         """
  141         url = "/redirect-to-%d" % self.port_two
  142         response = self.client.do_request("POST", url)
  143 
  144         self.assertEqual(http.OK, response.status)
  145         self.assertEqual(b"success_from_host_two", response.read())
  146 
  147         response = self.client.do_request("POST", "/success")
  148         self.assertEqual(http.OK, response.status)
  149         self.assertEqual(b"success_from_host_one", response.read())