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

Add matcher to check for charset. #253

Merged
merged 2 commits into from
Aug 23, 2019
Merged

Add matcher to check for charset. #253

merged 2 commits into from
Aug 23, 2019

Conversation

kierans
Copy link
Contributor

@kierans kierans commented Jun 26, 2019

Fixes #26

@austince
Copy link
Contributor

Hey @kierans, thanks for the contribution!

We're currently trying to upgrade our release process, which is why your travis builds are failing (but should be fixed with #252).

In the meantime, I'll run this locally when I get a chance. The only things I immediately see that we'll have to do is add this new method to the Typescript definitions (in ./types/index.d.ts) and fix the commit message to something like feat: add charset assertion.

git commit --amend
# then change the message
git push --force

@kierans
Copy link
Contributor Author

kierans commented Jun 27, 2019

@austince Thanks for the feedback.

As an aside, I like the way you have your docs in the Javascript source, however I haven't been able to find how you pull it all together into your README so I can update the README with the new assertion.

lib/http.js Outdated
Assertion.addMethod('charset', function (value) {
var headers = this._obj.headers;
var cs = charset(headers);
var expected = value.replace("-", "");
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this to transform 'utf-8' to 'utf8'? If so, I'm thinking that might be too specific a case. What do you think about using @keithamus's suggestion of node-mime instead? (See his comment here #26 (comment)) It seems to be better maintained and documented as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @austince. I did look the suggestion to use node-mime however as of version 2 charset() has been removed which is why I went with the charset module.

You do need to remove the '-' in order for the comparison to succeed. An improvement might be to convert value to all lowercase first. That satisfies input like:

  • "UTF-8"
  • "utf-8"
  • "UTF8"
  • "utf8"

and variants.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, that's unfortunate about node-mime, but makes sense.

It looks like from the charset code that utf-8 is a special case:

https://github.com/node-modules/charset/blob/dcc48ee609a9b8a137c68faad712c646fcaeda29/index.js#L54-L56

If we're going to use this module, do you think it might be better to handle it as a special case on our end as well, instead of removing the - from all inputs?

I think lowercasing the input value sounds like a good idea!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch there @austince.

@austince
Copy link
Contributor

austince commented Jun 27, 2019

Hey @kierans, thanks for updating the PR. We also recently removed the docs-to-readme script, as it didn't work as expected. We should think about a more standard way for building the docs in the future, but for now, you'll have to manually add it if that's alright.

Copy link
Contributor

@austince austince left a comment

Choose a reason for hiding this comment

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

The assertion method looks good, just a bit more test coverage would be great!

test/http.js Outdated
@@ -403,4 +403,13 @@ describe('assertions', function () {
}).should.throw('expected cookie \'name2\' to have value \'value\' but got \'value2\'');

});

it('#charset', function () {
Copy link
Contributor

@austince austince Jun 30, 2019

Choose a reason for hiding this comment

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

Can we also add tests where:

  • no encoding is specified in the Content-Type
  • no Content-Type is present

Just to test to the null case, since we've seen there might be some inconsistency in that lib.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@austince I think I've done what you wanted.

@austince
Copy link
Contributor

The tests are also passing locally, so looking good!

@kierans
Copy link
Contributor Author

kierans commented Jul 26, 2019

@austince Woah did I forget about this. Got distracted. Sorry.

I think I've addressed your concerns now.

Copy link
Contributor

@austince austince left a comment

Choose a reason for hiding this comment

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

This looks good to me! I'll request some others - thanks for looping back to this @kierans!

@austince austince requested a review from a team July 26, 2019 13:31
@kierans
Copy link
Contributor Author

kierans commented Aug 16, 2019

@austince Just wondering what's happening with this?

@austince
Copy link
Contributor

Hi @keithamus @vieiralucas @JamesMessinger, could someone take a look at this and #252 when they get a chance? Thanks!

Copy link
Member

@keithamus keithamus left a comment

Choose a reason for hiding this comment

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

This looks good. I don't relish the idea of adding yet another dependency but the code from charset looks fine. Let's fix the CI and then get this merged.

@kierans
Copy link
Contributor Author

kierans commented Aug 23, 2019

@keithamus I merged in master, however my job still failed. Looks like an issue with the CI setup to me.

@keithamus
Copy link
Member

Apologies @kierans, it indeed it. I'll merge this now and we can resolve CI in master

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

Successfully merging this pull request may close these issues.

Add easy testing for XML and Javascript content-types, and UTF-8 charset
3 participants