Comments (5)
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.
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.
@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 likemime.extension(flag(this, 'obj').headers['content-type']) === value
- Add a
charset
method which asserts using something likemime.charset.lookup(flag(this, 'obj').headers['content-type']) === value
- Refactor the
contentTypes
object to be something likeshortcutContentTypes
- 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.
Hey @nfreear did you get anywhere with this?
from chai-http.
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)
- How to check the contents of a binary response payload?
- Missing `exit` option in the API documentation, and the NodeJS API HOT 2
- Cannot read property 'headers' of undefined at Assertion.<anonymous> HOT 14
- Security Vulnerability: Update superagent Version to Fix! HOT 2
- Is there a way to share cookies between multiple agents?
- How do i POST xml request in chai? HOT 1
- Recommended way to extend the request class?
- Chai-http [POST] does not allow to send an Array on bodyParameters (API Testing & Javascript) HOT 1
- Superagent-proxy integration Failed
- Bump Chai-Http version to get package updates HOT 10
- Property 'request' does not exist on type 'Response'.ts (2339) HOT 3
- Issue with MongoDB ObjectId in tested function?
- Assertion error expending cookie
- [possibly bug] aws-sdk and multer-s3 with chai-http agent promise not resolved HOT 1
- Issues and PRs are piling. Are maintainers needed? HOT 1
- How to send gzip compressed body
- publish to npm actual version HOT 2
- Found CVE-2022-29622 in one of your dependent library- superagent
- How to send multiple json in post request using supertest
- Vulnerability in cookiejar CVE-2022-25901 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chai-http.