Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Polly does not respect <base> tag #315

Closed
nbfr opened this issue Mar 19, 2020 · 5 comments
Closed

Bug: Polly does not respect <base> tag #315

nbfr opened this issue Mar 19, 2020 · 5 comments
Labels
bug 🐞 Something isn't working dependency issue

Comments

@nbfr
Copy link

nbfr commented Mar 19, 2020

Prerequisites

A website running under a certain path e.g. "http://localhost:4200/some/feature"

Description

XHR requests made to a relative url, e.g. "rest/my-api" are altered by PollyJS to contain parts of the browser url. In this example "/some/rest/my-api".

Config

      Polly.register(XHRAdapter);
      Polly.register(PersisterStub);

      const polly = new Polly('mock', {
        adapters: ['xhr'],
        persister: 'PersisterStub'
      });

      const { server } = polly;

      server.get().passthrough();

Dependencies

{
    "@pollyjs/adapter-xhr": "~4.0.2",
    "@pollyjs/core": "~4.0.2",
    "@pollyjs/persister-rest": "~4.0.2",
}

Relevant Links

https://github.com/nbfr/pollyjs-dream-app

You can run the app with "npm run start" to run without PollyJS or
"npm run start-mock" to run with PollyJS.
Then open http://localhost:4200/some/feature.

Then check the network traffic. The client will send a request to "rest/my-api" which is changed by PollyJS to "some/rest/my-api".

Environment

Node.JS v10.16.3
win32 10.0.1832
npm 6.9.0

@jasonmit
Copy link
Collaborator

jasonmit commented Mar 22, 2020

Can confirm, the request goes out differently with Polly's XHR adapter enabled.

Will need to investigate further though.

@jasonmit
Copy link
Collaborator

jasonmit commented Mar 22, 2020

Okay, I believe this is caused by Polly not respecting the <base href="/"> in your template when building the request URL.

For now, a work around like this should unblock you:

import {Component, OnInit} from '@angular/core';
import {HttpClient} from '@angular/common/http';

function baseifyUrl(url: string) {
  let base = document.querySelector('base');

  if (base) {
    return base.getAttribute('href') + url;
  }

  return url;
}

@Component({
  selector: 'app-feature',
  templateUrl: './feature.component.html'
})
export class FeatureComponent implements OnInit {

  public requestPath = baseifyUrl('rest/my-api');

  constructor(private httpClient: HttpClient) {
  }

  ngOnInit(): void {
    this.httpClient.get(this.requestPath).subscribe((result) => console.log(result));
  }
}

@jasonmit jasonmit changed the title Incorrect handling of relative URLs Bug: Polly does not respect <base> tag Mar 22, 2020
@nbfr
Copy link
Author

nbfr commented Mar 23, 2020

@jasonmit : Thank you for quick feedback and the work around. For now I decided to extend the existing XHRAdapter.

class ProjectXHRAdapter extends XHRAdapter {
  static get type() {
    return 'adapter';
  }

  onConnect() {
    const basePath = new URL(document.baseURI).pathname;
    super.onConnect();
    const onCreate = (this as any).xhr.onCreate;
    (this as any).xhr.onCreate = xhrReq => {
      onCreate(xhrReq);
      const send = xhrReq.send;
      xhrReq.send = body => {
        if (!xhrReq.url.startsWith('/') && !xhrReq.url.startsWith('http://') && !xhrReq.url.startsWith('https://')) {
          xhrReq.url = basePath + xhrReq.url;
        }
        send(body);
      };
    };
  }
}

@jasonmit
Copy link
Collaborator

@offirgolan Don't believe there's anything in our lib that we should be doing extra to work around this - I feel like the fix probably belongs in nise. Thoughts?

@offirgolan
Copy link
Collaborator

I feel like the fix probably belongs in nise. Thoughts?

Agreed. @nbfr feel free to open up an issue with nise so it can be properly resolved upstream.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐞 Something isn't working dependency issue
Projects
None yet
Development

No branches or pull requests

3 participants