Content-length error in google cloud endpoints testing

北战南征 提交于 2019-12-04 05:39:06
Robert Dodd

This is a known error with App Engine.

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

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

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

1. Open apiserving.py

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. Locate the following section (line 467):

It should look like this:

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. Change it to this:

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. All done!

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

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