2014-06-14 103 views
6

我碰到下面的错误时,我想在我的代码来测试一个404 HTTP错误路径:内容长度错误

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

我已经创建了一个触发该行为最少的样品:

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'}) 

那么我做错了什么?

+0

那是完整的错误?我猜不是... – Veedrac

+0

我也是 - 当你在服务器上引发一个端点异常时它会发生。我发现这个http://trac.turbogears.org/ticket/2454,但仍然没有修复它 - 我会让你知道我的时间:) –

回答

9

这是App Engine已知的错误。

端点没有设置正确的Content-Length头,当你提出一个例外:

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

要修复它有包括在上面的链接一个diff文件,或者按照我的指示临时补丁它由你自己。

1.打开apiserving.py

在Mac系统中,你可以找到在文件:

/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测试将工作:)

+0

请参阅https://code.google.com/p/ googleappengine /问题/细节?ID = 12533个#C5 –