"Fossies" - the Fresh Open Source Software Archive

Member "buildbot-2.5.1/buildbot/test/unit/test_steps_http.py" (24 Nov 2019, 4422 Bytes) of package /linux/misc/buildbot-2.5.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.

    1 # This file is part of Buildbot.  Buildbot is free software: you can
    2 # redistribute it and/or modify it under the terms of the GNU General Public
    3 # License as published by the Free Software Foundation, version 2.
    4 #
    5 # This program is distributed in the hope that it will be useful, but WITHOUT
    6 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    7 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    8 # details.
    9 #
   10 # You should have received a copy of the GNU General Public License along with
   11 # this program; if not, write to the Free Software Foundation, Inc., 51
   12 # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
   13 #
   14 # Copyright Buildbot Team Members
   15 
   16 
   17 from twisted.internet import reactor
   18 from twisted.trial import unittest
   19 from twisted.web.resource import Resource
   20 from twisted.web.server import Site
   21 
   22 from buildbot.process import properties
   23 from buildbot.process.results import FAILURE
   24 from buildbot.process.results import SUCCESS
   25 from buildbot.steps import http
   26 from buildbot.test.util import steps
   27 from buildbot.test.util.misc import TestReactorMixin
   28 
   29 try:
   30     import txrequests
   31     assert txrequests
   32     import requests
   33     assert requests
   34 except ImportError:
   35     txrequests = requests = None
   36 
   37 
   38 # We use twisted's internal webserver instead of mocking requests
   39 # to be sure we use the correct requests interfaces
   40 
   41 class TestPage(Resource):
   42     isLeaf = True
   43 
   44     def render_GET(self, request):
   45         if request.uri == b"/404":
   46             request.setResponseCode(404)
   47             return b"404"
   48         elif request.uri == b"/header":
   49             return b"".join(request.requestHeaders.getRawHeaders(b"X-Test"))
   50         return b"OK"
   51 
   52 
   53 class TestHTTPStep(steps.BuildStepMixin, TestReactorMixin, unittest.TestCase):
   54 
   55     timeout = 3  # those tests should not run long
   56 
   57     def setUp(self):
   58         self.setUpTestReactor()
   59         if txrequests is None:
   60             raise unittest.SkipTest(
   61                 "Need to install txrequests to test http steps")
   62 
   63         # ignore 'http_proxy' environment variable when running tests
   64         session = http.getSession()
   65         session.trust_env = False
   66 
   67         # port 0 means random unused port
   68         self.listener = reactor.listenTCP(0, Site(TestPage()))
   69         self.port = self.listener.getHost().port
   70         return self.setUpBuildStep()
   71 
   72     def tearDown(self):
   73         http.closeSession()
   74         d = self.listener.stopListening()
   75         d.addBoth(lambda x: self.tearDownBuildStep())
   76         return d
   77 
   78     def getURL(self, path=""):
   79         return "http://127.0.0.1:%d/%s" % (self.port, path)
   80 
   81     def test_basic(self):
   82         url = self.getURL()
   83         self.setupStep(http.GET(url))
   84         self.expectLogfile(
   85             'log', "URL: %s\nStatus: 200\n ------ Content ------\nOK" % (url, ))
   86         self.expectLogfile('content', "OK")
   87         self.expectOutcome(result=SUCCESS, state_string="Status code: 200")
   88         return self.runStep()
   89 
   90     def test_404(self):
   91         url = self.getURL("404")
   92         self.setupStep(http.GET(url))
   93         self.expectLogfile(
   94             'log', "URL: %s\n ------ Content ------\n404" % (url, ))
   95         self.expectLogfile('content', "404")
   96         self.expectOutcome(
   97             result=FAILURE, state_string="Status code: 404 (failure)")
   98         return self.runStep()
   99 
  100     def test_POST(self):
  101         url = self.getURL("POST")
  102         self.setupStep(http.POST(url))
  103         self.expectOutcome(
  104             result=FAILURE, state_string="Status code: 405 (failure)")
  105         return self.runStep()
  106 
  107     def test_header(self):
  108         url = self.getURL("header")
  109         self.setupStep(http.GET(url, headers={"X-Test": "True"}))
  110         self.expectLogfile(
  111             'log', "URL: %s\nStatus: 200\n ------ Content ------\nTrue" % (url, ))
  112         self.expectOutcome(result=SUCCESS, state_string="Status code: 200")
  113         return self.runStep()
  114 
  115     def test_params_renderable(self):
  116         url = self.getURL()
  117         self.setupStep(http.GET(url, params=properties.Property("x")))
  118         self.properties.setProperty(
  119             'x', {'param_1': 'param_1', 'param_2': 2}, 'here')
  120         self.expectLogfile(
  121             'log', "URL: %s?param_1=param_1&param_2=2\nStatus: 200\n ------ Content ------\nOK" % (url, ))
  122         self.expectLogfile('content', "OK")
  123         self.expectOutcome(result=SUCCESS, state_string="Status code: 200")
  124         return self.runStep()