[英]404 in Protractor for known working non-root Angular pages
Why does the following code produce a 404 status code for http://localhost:4200/page yet a 200 status code for http://localhost:4200 ? 为什么下面的代码产生了404个状态码的http://本地主机:4200 /页又一个200个状态码的http://本地主机:4200 ?
When Protractor is running I'm able to open up another browser window and type http://localhost:4200/page into the address bar, press enter, and it works. 当量角器运行时,我可以打开另一个浏览器窗口,然后在地址栏中键入http:// localhost:4200 / page ,按Enter键,它可以工作。 But
request
in Protractor gives a 404. 但是量角器中的
request
给出了404。
import * as Request from 'request';
describe('Link', () => {
it('should work', async () => {
const href = 'http://localhost:4200'; // works
// const href = 'http://localhost:4200/page'; // doesn't work, even though this works outside of Protractor while Protractor is running
const statusCode = await new Promise<number>((resolve, reject) => {
Request(href, (error, response, body) => {
if (error) {
reject(error);
} else {
resolve(response.statusCode);
}
});
});
if (typeof(statusCode) !== 'number') {
throw new Error(`Failed to request ${href}`);
}
if (statusCode < 200 || statusCode >= 300) {
throw new Error(`Bad status code ${statusCode} for ${href}`);
}
});
});
Here's a complete, minimal, verifiable repro: https://drive.google.com/uc?export=download&id=1S2It1jA1bTR1hUoqdd_QC_Qa6BB3wnDS 这是一个完整的,最小的,可验证的副本: https ://drive.google.com/uc ? export = download & id = 1S2It1jA1bTR1hUoqdd_QC_Qa6BB3wnDS
ng serve
and navigate to http://localhost:4200/page directly in your browser of choice to observe the page does exist ng serve
并直接在您选择的浏览器中导航到http:// localhost:4200 / page ,以观察该页面是否存在 npm install
then ng e2e
to observe failure on http://localhost:4200/page URL npm install
然后ng e2e
观察http:// localhost:4200 / page URL上的失败 ng e2e
Note that Git history is included in the zip. 请注意,zip中包含Git历史记录。
The 404 is from a missing Accept: text/html
header. 404来自缺少的
Accept: text/html
标头。 Add that header and you get 200. 添加该标头,您将获得200。
As DublinDev pointed out it doesn't work in Postman, either, so it's not specifically a request
issue. 正如DublinDev所指出的那样,它在Postman中也不起作用,因此它不是特定的
request
问题。 But it does work in Chrome. 但是它确实可以在Chrome中运行。 That smells like a headers issue.
闻起来像标题问题。 Sure enough, after messing around with headers for a little bit I narrowed it down to this one.
果不其然,在将标题弄乱了一点之后,我将其缩小到了这个范围。
Here's the final product: 这是最终产品:
const statusCode = await new Promise<number>((resolve, reject) => {
Request({
url: href,
headers: {
'Accept': 'text/html'
}
}, (error, response, body) => {
if (error) {
reject(error);
} else {
resolve(response.statusCode);
}
});
});
So in summary: that header is necessary because the Angular site is hosted by Angular's dev server during E2E tests, and the dev server apparently has this quirk. 因此,总而言之:该标头是必需的,因为Angular站点是在E2E测试期间由Angular的dev服务器托管的,而dev服务器显然具有此怪癖。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.