简体   繁体   English

量角器中的404,用于已知的非根Angular工作页面

[英]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

  1. Run 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 ,以观察该页面是否存在
  2. Run npm install then ng e2e to observe failure on http://localhost:4200/page URL 运行npm install然后ng e2e观察http:// localhost:4200 / page URL上的失败
  3. Modify the E2E test to delay for a long time so that you can do this: 将E2E测试修改为延迟很长时间,以便您可以执行以下操作:
    1. ng e2e
    2. While Protractor is still running your long delayed test, navigate to http://localhost:4200/page directly in your browser of choice to observe the page does exist while Protractor is running 当量角器仍在运行长时间延迟的测试时,直接在选择的浏览器中导航至http:// localhost:4200 / page ,以观察该量角器在运行时确实存在

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM