1

私のコードは次のようになります。

self.testbed.init_blobstore_stub()
upload_url = blobstore.create_upload_url('/image')
upload_url = re.sub('^http://testbed\.example\.com', '', upload_url)

response = self.testapp.post(upload_url, params={
    'shopid': id,
    'description': 'JLo',
    }, upload_files=[('file', imgPath)])
self.assertEqual(response.status_int, 200)

なぜ404エラーが表示されるのですか? 何らかの理由で、アップロード パスがまったく存在しないようです。

4

2 に答える 2

3

これはできません。問題は、webtest (self.testapp がどこから来たかだと思います) がテストベッドのブロブストア機能でうまく動作しないことだと思います。この質問でいくつかの情報を見つけることができます。

unittest.TestCase私の解決策は、次のメソッドをオーバーライドして追加することでした。

def create_blob(self, contents, mime_type):
    "Since uploading blobs doesn't work in testing, create them this way."
    fn = files.blobstore.create(mime_type = mime_type,
                                _blobinfo_uploaded_filename = "foo.blt")
    with files.open(fn, 'a') as f:
        f.write(contents)
    files.finalize(fn)
    return files.blobstore.get_blob_key(fn)

def get_blob(self, key):
    return self.blobstore_stub.storage.OpenBlob(key).read()

こちらのソリューションも必要です。

通常は blobstore ハンドラーに対して get または post を行うテストでは、代わりに上記の 2 つのメソッドのいずれかを呼び出します。少しハックですが、機能します。

私が検討している別の解決策は、Selenium の HtmlUnit ドライバーを使用することです。これには、開発サーバーが実行されている必要がありますが、(副次的な利点として) ブロブストアと JavaScript の完全なテストを許可する必要があります。

于 2012-11-09T14:07:23.677 に答える
2

Kekito が正しいと思います。upload_url に直接 POST することはできません。

ただし、BlobstoreUploadHandler をテストする場合は、次の方法で、通常 blobstore から受信する POST 要求を偽造できます。ハンドラーが /handler にあると仮定します。

    import email
    ...

    def test_upload(self):
        blob_key = 'abcd'
        # The blobstore upload handler receives a multipart form request
        # containing uploaded files. But instead of containing the actual
        # content, the files contain an 'email' message that has some meta
        # information about the file. They also contain a blob-key that is
        # the key to get the blob from the blobstore

        # see blobstore._get_upload_content
        m = email.message.Message()
        m.add_header('Content-Type', 'image/png')
        m.add_header('Content-Length', '100')
        m.add_header('X-AppEngine-Upload-Creation', '2014-03-02 23:04:05.123456')
        # This needs to be valie base64 encoded
        m.add_header('content-md5', 'd74682ee47c3fffd5dcd749f840fcdd4')
        payload = m.as_string()
        # The blob-key in the Content-type is important
        params = [('file', webtest.forms.Upload('test.png', payload,
                                                'image/png; blob-key='+blob_key))]

        self.testapp.post('/handler', params, content_type='blob-key')

ブロブストアのコードを掘り下げて、それを理解しました。重要な点は、ブロブストアが UploadHandler に送信する POST 要求にファイル コンテンツが含まれていないことです。代わりに、ファイルに関するメタデータ (content-type、content-length、upload time、および md5) を含む「電子メール メッセージ」(つまり、電子メールのようにエンコードされた情報) が含まれています。また、ブロブストアからファイルを取得するために使用できるブロブ キーも含まれています。

于 2014-07-16T08:35:41.757 に答える