質問

I am confused as to how to pass cookies in a request using python webtest.

I have the following test:

def test_commenting_and_voting(self):
    https = {'wsgi.url_scheme': 'https'}
    users = []
    for user in USERS:
      resp_post = self.testapp.post_json('/user', user)
      users.append(resp_post.json.get('id'))

    self.testapp.post_json('/login/%s' % users[0],
                           {'password' : USERS[0]['password']},
                           extra_environ=https)
    print "testapp's view of the cookiejar"
    print self.testapp.cookies
    print "END"
    resp_post = self.testapp.post_json('/comment', {'value': ""})

and the following handler:

class CommentHandler(webapp2.RequestHandler):

    def get(self, id=None):
        get_from_urlsafe(self, id)

    @ndb.transactional
    def post(self, id=None):
        assert False, self.request.cookies

I am raising an error from the handler function in order to see the cookies. It appears that the cookies, although in the cookiejar within the webtest.TestApp side of things, are not being transmitted when making the wsgi request. So how do I get the cookies to transmit?

Using scent:
test_commenting_and_voting (test_models.test_Models) ... 
testapp's view of the cookiejar
{'secret': '58bd5cfd36e6f805de645e00f8bea9d70ae5398ff0606b7fde829e6732394bb7', 'session': 'agx0ZXN0YmVkLXRlc3RyIgsSD1VzZXJFbnRpdHlHcm91cBgBDAsSB1Nlc3Npb24YCww'}
END
WARNING:root:suspended generator transaction(context.py:941) raised AssertionError(<RequestCookies (dict-like) with values {}>)
ERROR:root:<RequestCookies (dict-like) with values {}>
Traceback (most recent call last):
  File "/home/stephen/bin/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  ... I removed some of the stacktrace here ....
  File "/home/stephen/work/seocomments/src/python/main.py", line 127, in post
    assert False, self.request.cookies
AssertionError: <RequestCookies (dict-like) with values {}>
----------------------------------------------------------------------
Ran 6 tests in 0.371s

FAILED (errors=1)
Failed - Back to work!
役に立ちましたか?

解決

Nevermind. The reason I wasn't seeing the cookies was that the cookies were being set as secure cookies, which means they only exist when using a secure connection. My test was using a insecure connection.

To make this work, change the request to the following:

self.testapp.post_json('/comment', 
                       {'value': ""}, 
                       extra_environ={'wsgi.url_scheme': 'https'})
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top