Git Product home page Git Product logo

Comments (11)

bboe avatar bboe commented on June 12, 2024

In this regard, if I remove the .encode('utf-8') part of the above line, that error goes away. Then it's simply a matching problem. The next issue is that the parts aren't sorting correctly by headers. That is resolved by prefixing Content-Disposition in sorted_by_headers as b'Content-Disposition'.

Once that's fixed, sadly everything matches but the upload file type itself. :( Below is a comparison of the parts that do not match:

{'content': "\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01\x00H\x00H\x00\x00\xff\xdb\x00C\x00\x06\x04\x05\x06\x05\x04\x06\x06\x05\x06\x07\x07\x06\x08\n\x10\n\n\t\t\n\x14\x0e\x0f\x0c\x10\x17\x14\x18\x18\x17\x14\x16\x16\x1a\x1d%\x1f\x1a\x1b#\x1c\x16\x16 , #&')*)\x19\x1f-0-(0%()(\xff\xc0\x00\x0b\x08\x01,\x01,\x01\x01\x11\x00\xff\xc4\x00\x15\x00\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08\xff\xc4\x00\x14\x10\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xda\x00\x08\x01\x01\x00\x00?\x00\xaa@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x7f\xff\xd9", 'headers': {'Content-Disposition': 'form-data; name="file"; filename="white-square.jpg"'}, 'encoding': 'utf-8'}
{'content': "\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\x00\x10JFIF\x00\x01\x01\x01\x00H\x00H\x00\x00\xef\xbf\xbd\xef\xbf\xbd\x00C\x00\x06\x04\x05\x06\x05\x04\x06\x06\x05\x06\x07\x07\x06\x08\n\x10\n\n\t\t\n\x14\x0e\x0f\x0c\x10\x17\x14\x18\x18\x17\x14\x16\x16\x1a\x1d%\x1f\x1a\x1b#\x1c\x16\x16 , #&')*)\x19\x1f-0-(0%()(\xef\xbf\xbd\xef\xbf\xbd\x00\x0b\x08\x01,\x01,\x01\x01\x11\x00\xef\xbf\xbd\xef\xbf\xbd\x00\x15\x00\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08\xef\xbf\xbd\xef\xbf\xbd\x00\x14\x10\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xef\xbf\xbd\xef\xbf\xbd\x00\x08\x01\x01\x00\x00?\x00\xef\xbf\xbd@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x7f\xef\xbf\xbd\xef\xbf\xbd", 'headers': {'Content-Disposition': 'form-data; name="file"; filename="white-square.jpg"'}, 'encoding': 'utf-8'}

from betamax_matchers.

bboe avatar bboe commented on June 12, 2024

So perhaps the problem is how betamax is saving the contents of the file in json. It records the file as beginning with \ufffd\ufffd\ufffd\ufffd\u0000\u0010JFIF which I believe is UTF-8 encoded.

The file actually begins with \xff\xd8\xff\xe0\x00\x10JFIF. It looks like betamax is doing a unicode convert with a replace on characters it doesn't know how to handle. At that point, encoding from utf-8 results in the following:

In [18]: u'\ufffd\ufffd\ufffd\ufffd\u0000\u0010JFIF`'.encode('utf-8')
Out[18]: '\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\x00\x10JFIF`'

Perhaps this section also needs to be base64 encoded like the response body?

from betamax_matchers.

sigmavirus24 avatar sigmavirus24 commented on June 12, 2024

So yes, we probably want something like coerce_content in betamax but for str.encode instead of str.decode. We can also recommend that users set preserve_exact_body_bytes=True for these, but there's nothing the matcher can do.

Would you make a PR with the changes you've described? They all sound valid and correct, I'm just not going to have much time today. I should be able to look it over later today.

from betamax_matchers.

bboe avatar bboe commented on June 12, 2024

Yes, I will work on a PR for this issue. Thanks!

from betamax_matchers.

sigmavirus24 avatar sigmavirus24 commented on June 12, 2024

@bboe just noticed this hasn't been updated. Do you still have plans to tackle this or should I?

from betamax_matchers.

bboe avatar bboe commented on June 12, 2024

Thanks for checking. I completely forgot about this issue. Unfortunately, my time for non-work related open source projects has dwindled, so my need for such a PR doesn't really exist (until I find a work-related reason to need such a betamax matcher).

from betamax_matchers.

bboe avatar bboe commented on June 12, 2024

@sigmavirus24 it would seem the reason why I wrote a custom matcher to begin with was similar to this issue.

Here's code that does reproduce an issue with the jsonmatcher:

#!/usr/bin/env python                                                                                                                                                              
from __future__ import print_function
import json
import sys

from betamax_matchers.json_body import JSONBodyMatcher
import betamax
import requests


def main():
    print('  Python: {:>6}'.format(sys.version.split()[0]))
    print(' Betamax: {:>6}'.format(betamax.__version__))
    print('Requests: {:>6}'.format(requests.__version__))

    betamax.Betamax.register_request_matcher(JSONBodyMatcher)

    with requests.Session() as session:
        with betamax.Betamax(session, cassette_library_dir='.') as recorder:
            recorder.use_cassette(
                'test', match_requests_on=['json-body'])
            response = session.post('https://httpbin.org/post', json={'a': 2})

    import pprint
    pprint.pprint(response.json())


if __name__ == '__main__':
    sys.exit(main())

Output:

 Betamax:  0.7.2
Requests: 2.11.0
Traceback (most recent call last):
  File "./betamax_issue.py", line 28, in <module>
    sys.exit(main())
  File "./betamax_issue.py", line 21, in main
    response = session.post('https://httpbin.org/post', json={'a': 2})
  File "/Users/bboe/.venv/praw/lib/python3.5/site-packages/requests/sessions.py", line 514, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "/Users/bboe/.venv/praw/lib/python3.5/site-packages/requests/sessions.py", line 471, in request
    resp = self.send(prep, **send_kwargs)
  File "/Users/bboe/.venv/praw/lib/python3.5/site-packages/requests/sessions.py", line 581, in send
    r = adapter.send(request, **kwargs)
  File "/Users/bboe/.venv/praw/lib/python3.5/site-packages/betamax/adapter.py", line 123, in send
    interaction = current_cassette.find_match(request)
  File "/Users/bboe/.venv/praw/lib/python3.5/site-packages/betamax/cassette/cassette.py", line 133, in find_match
    if not interaction.match(curried_matchers):
  File "/Users/bboe/.venv/praw/lib/python3.5/site-packages/betamax/cassette/interaction.py", line 58, in match
    return all(m(request) for m in matchers)
  File "/Users/bboe/.venv/praw/lib/python3.5/site-packages/betamax/cassette/interaction.py", line 58, in <genexpr>
    return all(m(request) for m in matchers)
  File "/Users/bboe/.venv/praw/lib/python3.5/site-packages/betamax_matchers/json_body.py", line 26, in match
    request_json = json.loads(request.body) if request.body else None
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/__init__.py", line 312, in loads
    s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'

Since I'm stuck on this, I will look into making a PR to fix it.

from betamax_matchers.

bboe avatar bboe commented on June 12, 2024

This morning I am unable to reproduce the same issue I had with the json matcher. I'm going to close this issue for now since I don't have anything I'm working on that is currently having issue.

from betamax_matchers.

bboe avatar bboe commented on June 12, 2024

Looks like I'm getting this issue again. Running that example above in the first run I get:

  Python:  3.5.2
 Betamax:  0.8.0
Requests: 2.12.1
{'args': {},
 'data': '{"a": 2}',
 'files': {},
 'form': {},
 'headers': {'Accept': '*/*',
             'Accept-Encoding': 'gzip, deflate',
             'Content-Length': '8',
             'Content-Type': 'application/json',
             'Host': 'httpbin.org',
             'User-Agent': 'python-requests/2.12.1'},
 'json': {'a': 2},
 'origin': '68.6.101.75',
 'url': 'https://httpbin.org/post'}

A subsequent run gives me:

  Python:  3.5.2
 Betamax:  0.8.0
Requests: 2.12.1
Traceback (most recent call last):
  File "example.py", line 29, in <module>
    sys.exit(main())
  File "example.py", line 22, in main
    response = session.post('https://httpbin.org/post', json={'a': 2})
  File "/Users/bboe/.venv/prawcore/lib/python3.5/site-packages/requests-2.12.1-py3.5.egg/requests/sessions.py", line 535, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "/Users/bboe/.venv/prawcore/lib/python3.5/site-packages/requests-2.12.1-py3.5.egg/requests/sessions.py", line 488, in request
    resp = self.send(prep, **send_kwargs)
  File "/Users/bboe/.venv/prawcore/lib/python3.5/site-packages/requests-2.12.1-py3.5.egg/requests/sessions.py", line 609, in send
    r = adapter.send(request, **kwargs)
  File "/Users/bboe/.venv/prawcore/lib/python3.5/site-packages/betamax-0.8.0-py3.5.egg/betamax/adapter.py", line 123, in send
  File "/Users/bboe/.venv/prawcore/lib/python3.5/site-packages/betamax-0.8.0-py3.5.egg/betamax/cassette/cassette.py", line 133, in find_match
  File "/Users/bboe/.venv/prawcore/lib/python3.5/site-packages/betamax-0.8.0-py3.5.egg/betamax/cassette/interaction.py", line 58, in match
  File "/Users/bboe/.venv/prawcore/lib/python3.5/site-packages/betamax-0.8.0-py3.5.egg/betamax/cassette/interaction.py", line 58, in <genexpr>
  File "/Users/bboe/src/betamax_matchers/betamax_matchers/json_body.py", line 31, in match
    request_json = json.loads(request.body) if request.body else None
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/__init__.py", line 312, in loads
    s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'

I'm using the master version of betamax_matchers. Since I'm again blocked on it I will look into a fix.

from betamax_matchers.

sigmavirus24 avatar sigmavirus24 commented on June 12, 2024

This is really weird.

from betamax_matchers.

bboe avatar bboe commented on June 12, 2024

I think the reason why I stopped getting this particular issue is I had requests fixed to 2.10 for testing:praw-dev/prawcore@0c5d28e.

from betamax_matchers.

Related Issues (3)

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.