GAE: testing download of blobs with testbed and we

2019-06-23 23:51发布

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条回答
戒情不戒烟
2楼-- · 2019-06-24 00:07

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/

查看更多
登录 后发表回答