Google Cloud端点测试中的内容长度错误 [英] Content-length error in google cloud endpoints testing

查看:91
本文介绍了Google Cloud端点测试中的内容长度错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我想在代码中测试404 HTTP错误路径时,都会出现以下错误:

I get the following error whenever I want to test a 404 HTTP error path in my code:

AssertionError:Content-Length与实际的app_iter长度(512!= 60)不同

AssertionError: Content-Length is different from actual app_iter length (512!=60)

我创建了一个触发此行为的最小样本:

I have created a minimal sample that triggers this behavior:

import unittest
import endpoints
from protorpc import remote
from protorpc.message_types import VoidMessage
import webtest

@endpoints.api(name='test', version='v1')
class HelloWorld(remote.Service):
    @endpoints.method(VoidMessage, VoidMessage,
                      path='test_path', http_method='POST',
                      name='test_name')
    def test(self, request):
        raise endpoints.NotFoundException("Not found")

class AppTest(unittest.TestCase):
    def setUp(self):
        app = endpoints.api_server([HelloWorld])
        self.testapp = webtest.TestApp(app)

    # Test the handler.
    def testHelloWorldHandler(self):
        response = self.testapp.post('/_ah/spi/HelloWorld.test', extra_environ={
            'SERVER_SOFTWARE': 'Development/X', 'CONTENT_TYPE': 'application/json'})

那我在做什么错了?

推荐答案

这是App Engine的一个已知错误.

This is a known error with App Engine.

引发异常时,端点没有设置正确的Content-Length标头:

Endpoints does not set the correct Content-Length header when you raise an exception:

https://code.google.com/p/googleappengine/issues/detail?id = 10544

要修复此问题,上面的链接中包含一个diff文件,或者按照我的指示由您自己临时对其进行修补.

To fix it there is a diff file included in the link above, or follow my instructions to temporarily patch it by yourself.

1.打开apiserving.py

1. Open apiserving.py

在Mac上,您可以在以下位置找到文件:

On a mac you can find the file at:

/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/endpoints-1.0/endpoints

2.找到以下部分(第467行):

它应该看起来像这样:

headers_dict = dict([(k.lower(), v) for k, v in headers])
if self.__is_json_error(status, headers_dict):
  status, body = self.protorpc_to_endpoints_error(status, body)

3.更改为此:

headers_dict = dict([(k.lower(), v) for k, v in headers])
if self.__is_json_error(status, headers_dict):
  pre_body_length = len(body)
  status, body = self.protorpc_to_endpoints_error(status, body)
  post_body_length = len(body)
  if pre_body_length != post_body_length:
    for index, header in enumerate(headers):
      header_key, _header_value = header
      if header_key == 'content-length':
        headers[index] = (header_key, str(post_body_length))
        break

4.全部完成!

端点将返回正确的Content-Length,WebOb将很高兴,并且您的API测试将正常进行:)

Endpoints will return the correct Content-Length, WebOb will be happy and your API tests will be working :)

这篇关于Google Cloud端点测试中的内容长度错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆