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

UTF-8 Encoding error when matcher fails #668

Open
andywatson00 opened this issue Dec 9, 2016 · 5 comments
Open

UTF-8 Encoding error when matcher fails #668

andywatson00 opened this issue Dec 9, 2016 · 5 comments

Comments

@andywatson00
Copy link

Not a major bug but I lost a lot of time trying to figure out why my spec was failing, so decided it was worth posting.

So I have a spec using expect(WebMock).to have_requested(:post, url).with(body: hash_including(expected_values)) and my body included a string with an 'à' character.

So my spec was failing due to a completely mundane reason but I had no idea because i kept getting this error

Encoding::CompatibilityError:
       incompatible character encodings: UTF-8 and ASCII-8BIT

After a lot of debugging it turned out that the problem was not in the matcher as I expected but actually in request_execution_verifier.rb here

def failure_message_phrase(is_negated=false)
    negation = is_negated ? "was not" : "was"
    text = "The request #{request_pattern} #{negation} expected to execute #{quantity_phrase(is_negated)}but it executed #{times(times_executed)}"
    text << self.class.executed_requests_message
    text
end

Specifically text << self.class.executed_requests_message.

Before that line is executed text.encoding = #<Encoding:UTF-8> and
self.class.executed_requests_message.encoding = #<Encoding:ASCII-8BIT> hence the error.

It may be worth catching this or possibly forcing the encoding.

@bblimke
Copy link
Owner

bblimke commented Jan 1, 2017

@andywatson00 I'm not getting that problem. What http client and what ruby version do you use? Could you please provide a failing code example?

@lazylester
Copy link

I am seeing this issue, and I've been working for a few hours to make a failing test case, with little success. The conditions in my app that trigger the problem don't seem to reproduce the problem in the webmock test suite.

I have gained some insight, however. So I'd like to share it in case someone can suggest how to construct a failing test for this.

During a single test example (I'm using rspec) there may be several requests mocked. If any of them fails to meet the matcher criterion, a text message is shown which accumulates all the stubs and all the actual requests that occurred. This is accumulated in the RequestRegistry#to_s method.

Within this error message are included the body contents of post requests. Here is the problem:
if an earlier request contains a post body in utf8 encoding and a later request contains a post body in ascii encoding, it may not be possible to concatenate them, and this is when the error occurs. The error message is:

Encoding::CompatibilityError:
incompatible character encodings: UTF-8 and ASCII-8BIT

It appears to be data dependent, however. In irb I can concatenate utf8 and ascii strings, but I think there may be some strings for which a common encoding is not possible? Does that make sense? I'll keep trying to make a test case, but this triggers anyone's insight, please let me know.

@lazylester
Copy link

No sooner did I write the previous comment than I found a way to reproduce the problem. It's not as compact as one might like, I had to incorporate ActiveSupport, and I'm pulling in a utf8 string for post body from a file, just b/c I know it triggers the problem and I haven't found anything shorter to do the same.

The failing test is in my fork at https://github.com/lazylester/webmock.git, run rake test to show the problem. Let me know if I can be of any assistance fixing it. Or maybe you'll decide not to fix it as it is really just a misleading error message.

@bblimke
Copy link
Owner

bblimke commented Jun 17, 2017

@lazylester Thank you for investigating this issue. Great job.

It would be great to fix this issue, assuming the problem is in WebMock,
to avoid people having the same issue in the future.

Unfortunately I don't have time to investigate it any further these days,
but I'd be happy to accept a pull request if you found a solution.

@lazylester
Copy link

#705 fixes the problem.

I decided to fix this as I'm running into it yet again. It comes up in the context of integration testing when there are certain contents in post messages.

It's a legitimate question whether this is actually a webmock problem. I'm assuming here that webmock should be able to correctly display a diagnostic message regardless of what is in the body of the post requests. Happy to hear any thoughts to the contrary.

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

No branches or pull requests

4 participants