GAE: testing download of blobs with testbed and webtest

 ̄綄美尐妖づ 提交于 2019-12-12 10:56:14

问题


I'm using the blobstore with my Google App Engine app, and everything is working fine on the production server and the development server. Testing with testbed and webtest, however, isn't working...

In my tests, the blob exists as I can access it like this:

blob = self.blobstore_stub.storage._blobs[key]

When I try to download a blob in my tests like this

response = self.app.get("/blob-download/2")

my blobstore download handler never gets called and I get a 404 error (but the link works on the dev or prod servers).

I suspect this is an error with testbed or webtest...

Any ideas as to what I might be doing wrong, or if this is an error with testbed/webtest what a good work around might be so that I can test this part of my code?


Here is some info about how I am setting up my tests.

import unittest
from webtest import TestApp
from google.appengine.ext import db, testbed
from google.appengine.api import users
from google.appengine.api import apiproxy_stub_map

class ExampleTests(unittest.TestCase):

    def setUp(self):
        self.testbed = testbed.Testbed()
        self.testbed.setup_env(app_id="stv")
        self.testbed.activate()
        self.testbed.init_datastore_v3_stub()
        self.testbed.init_taskqueue_stub()
        self.testbed.init_mail_stub()
        self.testbed.init_blobstore_stub()
        self.app = TestApp(main.application)
        apiproxy_stub_map.apiproxy.GetStub("datastore_v3").Clear()
        self.taskqueue_stub = apiproxy_stub_map.apiproxy.GetStub('taskqueue')
        self.mail_stub = apiproxy_stub_map.apiproxy.GetStub('mail')
        self.blobstore_stub = apiproxy_stub_map.apiproxy.GetStub('blobstore')

   def testBlob(self):
        # create blob using files.blobstore.create
        response = self.app.get("/blob-download/2") # This returns 404
        self.assertEqual(response.body, "content of blob") # This fails

This is the relevant portion of app.yaml:

handlers:
- url: /.*
  script: main.application

This is the relevant portion of main.py:

application = webapp2.WSGIApplication(
    [
     ('/blob-download/([^/]+)?', views.BlobDownload),
    ]

回答1:


It's hard to tell about the routing without having the routing from main.application and app.yaml available.

I suspect that you configured "/blob-download" in app.yaml of which webtest is unaware, it only knows about the routing you configured in main.application.

update: Now that we now app.yaml is not the cause, let's move on. What would help is to see your handler. Blobstore serving responses are handled differently then the usual responses. You, as a developer, add the blob key as a header to the response. The App Engine backend checks for this header end and if it finds it takes over the serving of the blob. You can check out the dev_appserver implementation here: http://code.google.com/p/googleappengine/source/browse/trunk/python/google/appengine/tools/dev_appserver_blobstore.py#214.

This means you can't actually test serving of blobs without having dev_appserver or appserver processing the requests - which means testbed + webtest won't help you here (it doesn't explain the 404 though).

What you could do is run a full end-to-end test, for example with gaedriver: http://code.google.com/p/gaedriver/



来源:https://stackoverflow.com/questions/9303019/gae-testing-download-of-blobs-with-testbed-and-webtest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!