def test_postPage_does_not_repost_on_redirect(self):
        # By default, if the POST redirects, _postPage leaves requests to
        # handle it in the normal, RFC-compliant way.
        base_url = "http://example.com/"
        form = self.factory.getUniqueString()
        target = self.factory.getUniqueString()
        fake_form = "<bugzilla>%s</bugzilla>" % self.factory.getUniqueString()
        bugtracker = ExternalBugTracker(base_url)
        transaction.commit()
        responses.add("POST",
                      base_url + form,
                      status=302,
                      headers={"Location": base_url + target})
        responses.add("GET", base_url + target, body=fake_form)

        bugtracker._postPage(form, {})

        requests = [call.request for call in responses.calls]
        self.assertThat(
            requests,
            MatchesListwise([
                MatchesStructure.byEquality(method="POST",
                                            path_url="/" + form),
                MatchesStructure.byEquality(method="GET",
                                            path_url="/" + target),
            ]))
    def test_postPage_can_repost_on_redirect(self):
        # Some pages (that means you, BugZilla bug-search page!) can
        # redirect on POST, but without honouring the POST.  Standard
        # requests behaviour is to redirect to a GET, but if the caller
        # says it's safe, _postPage can re-do the POST at the new URL.
        base_url = "http://example.com/"
        form = self.factory.getUniqueString()
        target = self.factory.getUniqueString()
        fake_form = "<bugzilla>%s</bugzilla>" % self.factory.getUniqueString()
        bugtracker = ExternalBugTracker(base_url)
        transaction.commit()
        responses.add("POST",
                      base_url + form,
                      status=302,
                      headers={"Location": base_url + target})
        responses.add("POST", base_url + target, body=fake_form)

        bugtracker._postPage(form, form={}, repost_on_redirect=True)

        requests = [call.request for call in responses.calls]
        self.assertThat(
            requests,
            MatchesListwise([
                MatchesStructure.byEquality(method="POST",
                                            path_url="/" + form),
                MatchesStructure.byEquality(method="POST",
                                            path_url="/" + target),
            ]))
 def test_postPage_sends_host(self):
     # When posting, a Host header is sent.
     base_host = 'example.com'
     base_url = 'http://%s/' % base_host
     bugtracker = ExternalBugTracker(base_url)
     transaction.commit()
     responses.add("POST", base_url + "some-url")
     bugtracker._postPage('some-url', {'post-data': 'here'})
     self.assertThat(
         responses.calls[-1].request,
         MatchesStructure(
             headers=ContainsDict({
                 "User-Agent": Equals(LP_USER_AGENT),
                 "Host": Equals(base_host),
             })))
 def test_postPage_returns_response_page(self):
     # _postPage posts, then returns the page text it gets back from
     # the server.
     base_url = "http://example.com/"
     form = self.factory.getUniqueString()
     fake_form = "<bugzilla>%s</bugzilla>" % self.factory.getUniqueString()
     bugtracker = ExternalBugTracker(base_url)
     transaction.commit()
     responses.add("POST", base_url + form, body=fake_form)
     self.assertEqual(fake_form, bugtracker._postPage(form, {}).text)