msw icon indicating copy to clipboard operation
msw copied to clipboard

feat: add HttpResponse.html()

Open scruffymongrel opened this issue 1 year ago • 6 comments

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.

scruffymongrel avatar Apr 28 '24 10:04 scruffymongrel

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).

kettanaito avatar Apr 29 '24 13:04 kettanaito

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 :)

scruffymongrel avatar Apr 29 '24 13:04 scruffymongrel

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

kettanaito avatar May 02 '24 10:05 kettanaito

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.

scruffymongrel avatar May 13 '24 07:05 scruffymongrel

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.

kettanaito avatar May 13 '24 18:05 kettanaito

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.

scruffymongrel avatar May 14 '24 05:05 scruffymongrel

Documentation added in https://github.com/mswjs/mswjs.io/pull/415.

kettanaito avatar Aug 28 '24 16:08 kettanaito

Released: v2.4.0 🎉

This has been released in v2.4.0!

Make sure to always update to the latest version (npm i msw@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

kettanaito avatar Aug 28 '24 16:08 kettanaito