【发布时间】:2018-10-31 08:12:15
【问题描述】:
自从过去两天以来,我面临着一个相当令人困惑的问题。我正在开发一个文档管理系统,该系统使用从 SOLR 提取数据的 API。数据大约为 15Mbs,并提取了超过 4000 多个文档的记录。 API 有这种格式的响应 -
{
"documents": [
{
id: 123,
some_field: "abcd",
some_other_field: "abcdef"
},
{
id: 124,
some_field: "abcd1",
some_other_field: "abcdef1"
}
]
}
在浏览器中一切正常。如果我在 Chrome 或 Firefox 浏览器中点击端点,它会给我正确的输出,并且我能够看到 JSON 输出。
但是,如果我尝试使用 Java 或 JS 代码访问同一个 API 端点 - 响应代码为 200,但控制台(终端或 Eclipse)中的输出显示像 \u0089 \u0078 U+0080 这样的 unicode 字符 - 所有输出都在此方式,并且由于 API 获取了大约 4000 多条记录,控制台有点填充所有这些 unicode 字符。
我在浏览器发出的请求和代码之间看到的唯一区别是,在浏览器中我可以看到Content-Encoding : gzip,而我在我编写的代码中找不到这个标头。例如-在JS代码中,通过Chakram框架,我可以检查
expect(response).to.be.encoded.with.gzip
提到了here。但是,这会返回一个失败,说明 expected undefined to match gzip
我在这里缺少什么?这是与编码/解码有关的东西还是完全不同的东西?
编辑 1:在 Chrome 的Network 标签中看到的响应标头:
cache-control: max-age=0, private, must-revalidate, max-age=315360000
content-encoding: gzip
content-type: application/json; charset=utf-8
date: Tue, 22 May 2018 06:07:26 GMT
etag: "a07eb7c1eef4ab97699afc8d61fb9c5d"
expires: Fri, 19 May 2028 06:07:26 GMT
p3p: CP="NON CUR OTPi OUR NOR UNI"
server: Apache
Set-Cookie : some_cookie
status: 200 OK
strict-transport-security:
transfer-encoding: chunked
vary: Accept-Encoding
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-request-id: abceefr4-1234-acds-100b-d2bef2413r47
x-runtime: 3.213943
x-ua-compatible: chrome=1
x-xss-protection: 1; mode=block
在 Chrome 的 Network 标签中看到的请求标头
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Connection: keep-alive
Cookie: some_cookie
Host: abcd.bcd.com
IV_USER: demouser123
IV_USER_L: demouser123
MAIL: demouser@f.com
PERSON_ID: 123
Referer: http://abcd.bcd.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36
X-CSRF-TOKEN: some_csrf_token
编辑 2:我正在使用的测试
describe('Hits required API',()=>{
before(()=>{
return chakram.wait(api_response = chakram.get(url,options));
});
it('displayes response',()=>{
return api_response.then((t_resp)=>{
console.log(JSON.stringify(t_resp));
expect(t_resp).to.have.header('Content-Encoding','gzip');
});
});
【问题讨论】:
-
您能否向我们展示请求的所有标头——请求和响应? (从浏览器复制或使用 cURL)
-
请参阅编辑@ThomasEdwards
-
你能告诉我们整个测试吗?
-
见编辑 2 @ThomasEdwards。此请求是使用
Chakram发出的 -
t_resp的控制台日志打印出什么?它是否包含任何标题?
标签: javascript api encoding web-api-testing