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

feat: add HttpResponse.html() #2140

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

scruffymongrel
Copy link

Add the ability to return html responses, enabling users to mock hypermedia APIs.

It mimics HttpResponse.xml(), except it creates a Response instance with the Content-Type: text/html.

@kettanaito
Copy link
Member

Hey, @scruffymongrel! Thanks for proposing this.

I will have to think about this for a while. The .html() method is certainly useful, I just don't know what's the stance regarding custom helper methods (more like, I have to define one).

@scruffymongrel
Copy link
Author

Thanks for taking a look. I'm not sure what you mean by "the stance regarding custom helper methods", but let me know if there's anything I can do to help :)

@kettanaito
Copy link
Member

@scruffymongrel can you please share your use case for this? When would you want to respond with HTML to a fetch request?

@scruffymongrel
Copy link
Author

Sorry for the delay in getting back to you -- this fell off my list and I just remembered I owed you a response!

I was originally intending to use MSW to work with htmx, mocking responses which would be chucks of HTML. Unfortunately htmx uses XHR rather than fetch, so I'm guessing that this would be a bad fit. There was discussion around htmx moving to fetch in future versions but it doesn't seem to a priority at the moment.

If I've missed something and there is a way for MWS to work with XHR, I'd be keen to know more. Otherwise, feel free to close this PR.

@kettanaito
Copy link
Member

Unfortunately htmx uses XHR rather than fetch, so I'm guessing that this would be a bad fit.

Why? MSW is client-agnostic and works with XHR as well as it does with fetch or http.request (speaking of Node.js). You should certainly give it a try, you don't need anything special to make MSW work with XHR. If it doesn't, please submit a reproduction repository, I will take a look at what's wrong.

I think HttpResponse.html() has a place to be. Will look at the PR, check the tests, and let's have this one merged.

Copy link
Member

@kettanaito kettanaito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fantastic. Thank you for your work on this, @scruffymongrel. Marking it as a release candidate. Expect it to land in the next minor version. I will look at pending bugfixes, release those first, then this.

@scruffymongrel
Copy link
Author

MSW is client-agnostic and works with XHR

Excellent! Was disappointed when I thought MSW was restricted to just fetch, so this is great news. Glad to hear that HttpResponse.html() is an appropriate feature for MSW and that I'll be able use it with htmx. Thanks for taking a look and for setting me straight on applicability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants