Giter Club home page Giter Club logo

Comments (5)

keithamus avatar keithamus commented on June 29, 2024

Hey @nfreear thanks for the issue! You're totally right, we could extend these a bunch. If you fancy making a pull request - I'd love to see it.

For some ideas about a PR - you could use node-mime and override the var contentTypes declaration in http.js to use node-mimes list. (Node mime is a dependency of SuperTest/Agent anyway, so you wouldn't be introducing a new dependency per se.)

So the final code might look something like:

var contentTypes = require('mime').types

This may be overkill though, as currently node-mime has 998 mime-types in mime.types.

As for charsets - personally I think they should be a little more explicit, as to not conflict with mimetypes, and because the assertion method would be easier:

expect(res).to.have.charset('utf-8')

The charset method could then use node-mime's mime.charset.lookup method and pass in the Content-Encoding header, something like

chai.addMethod('charset', function (value) {

   this.assert(
      mime.charset.lookup(flag(this, 'obj')).toLowerCase() === value.toLowerCase()
   );
});

Thoughts?

from chai-http.

nfreear avatar nfreear commented on June 29, 2024

Hi Keith,

Thanks for the detailed response.

I agree we should leverage node-mimetypes - there's not much point adding content-types piecemeal. As long has the 998 mime-types don't have a detrimental affect on performance...!

I'd suggest, use node-mimetypes, possibly override one or two (make text/javascript the expected type for Javascript - not 'application/javascript'); possibly add the odd alias - javascript as well as js (readability); and add a mime method. So,

    expect(res).to.be.js
    expect(res).to.be.javascript                        // Expect 'text/javascript'
    expect(res).to.have.mime('application/javascript')  // Override defaults/ easy extensibility.
    expect(res).to.have.mime('application/vnd.example')

And, I think you've got the charset API nailed,

    expect(res).to.have.charset('utf-8')                // Case in-sensitive check.

How does that sound? I'm happy to continue implementing - based on your addMethod snippet.

Best wishes,

Nick

from chai-http.

keithamus avatar keithamus commented on June 29, 2024

@nfreear you've all but nailed it - great ideas! We can use node-mime and have both .mime and .charset assertions for the broad cases, and have a couple of convenient aliases which call out to .mime, so here's my suggestion:

  • Add a mime method which asserts using something like mime.extension(flag(this, 'obj').headers['content-type']) === value
  • Add a charset method which asserts using something like mime.charset.lookup(flag(this, 'obj').headers['content-type']) === value
  • Refactor the contentTypes object to be something like shortcutContentTypes - perhaps as an array of content type names?
  • Refactor the checkContentType method to use the .to.be.mime assertion

So the end result will look something like:

expect(res).to.have.charset('utf-8') 
expect(res).to.have.mime('js')
expect(res).to.be.js // shortcut for .to.have.mime('js')

.mime could either use node-mime's lookup list (so pass extension name rather than full mimetype) or the full explicit mimetype, perhaps it could do both by first looking up the value through node-mime and then defaulting to just using it explicitly? This way you could do both

expect(res).to.have.mime('css')
expect(res).to.have.mime('text/css') // same assertion as above

from chai-http.

keithamus avatar keithamus commented on June 29, 2024

Hey @nfreear did you get anywhere with this?

from chai-http.

jameswlane avatar jameswlane commented on June 29, 2024

Instead of opening a new issue I am just commenting on here.

I would really love to see XML support for plugin. Started working on a new project and I thought this chai-http would be perfect for testing the API, but it's in XML.

I would love to help contribute to get XML support if needed.

from chai-http.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.