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

Adding an in-memory stub version of treq #96

Merged
merged 26 commits into from Jul 13, 2015
Merged

Adding an in-memory stub version of treq #96

merged 26 commits into from Jul 13, 2015

Conversation

cyli
Copy link
Member

@cyli cyli commented Jul 7, 2015

Thanks to @glyph for the suggestion of using the request traversal agent from https://github.com/rackerlabs/mimic, which means that treq actually operates and produces a real response, so we don't have to make a verified fake response, and for help debugging HTTPS and request issues.

Part of addressing #27.

Was going to write the docs and tutorial next if this looks good.

@cyli
Copy link
Member Author

cyli commented Jul 7, 2015

This is failing due to #97

@glyph
Copy link
Member

glyph commented Jul 8, 2015

Just a quick point of order to track provenance; @cyli is a Rackspace employee, and although this code originates from the APL2-licensed Mimic, the files in question have only been touched by other Rackspace employees and so she is authorized to relicense it as MIT to contribute it upstream to Treq.

@glyph
Copy link
Member

glyph commented Jul 8, 2015

I just merged master to update the Travis results.

self._agent = agent
self._cookiejar = cookiejar or cookiejar_from_dict({})
self._bodyproducer = bodyproducer
Copy link
Member

Choose a reason for hiding this comment

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

This attribute should probably be called something more like dataToBodyProducer since it is not itself an IBodyProducer but rather the way that you get one.

@codecov-io
Copy link

Current coverage is 95.82%

Merging #96 into master will increase coverage by +0.51% as of b941516

Coverage Diff

@@            master     #96   diff @@
======================================
  Files           17      19     +2
  Stmts         1259    1411   +152
  Branches        97     112    +15
  Methods          0       0       
======================================
+ Hit           1200    1352   +152
  Partial         19      19       
  Missed          40      40       

Powered by Codecov

@glyph
Copy link
Member

glyph commented Jul 8, 2015

I think the codecov report is in fact noticing a problem - there are more 'partial' and 'missed' lines in this branch. That said - who added codecov to this repo?

@hynek
Copy link
Member

hynek commented Jul 8, 2015

#100

@cyli
Copy link
Member Author

cyli commented Jul 8, 2015

There are a couple of missing lines of coverage in testing.py:

  • abortConnection's call to loseConnection: I guess abortConnection is never called - I'm not sure how to trigger that in the tests - will that work if we just cancel an outstanding request?
  • SynchronousProducer.stopProducing is only partially covered? I'm not entirely sure how to trigger this.
  • IStringResponseStubs.get_response_for - I'd expect this to not be covered at all, since it's an interface - I'm not sure why it's partially covered. I guess we should add a # pragma no-cover or something to this line?
  • the ImportError from trying to import twisted.web.iweb.IAgent is not covered - this is odd, since the tests are running twisted < 13.1.0, which means that there should be an ImportError importing it for those test environments - are the coverage reports from all the different environments merged by codecov? (@hynek) UPDATE: NM, looks like the tox changes removed testing Twisted < 13.2.0 - I'll remove this try/except block.
  • The HasHeaders.__repr__ function is uncovered - I'll add a test for that.
  • test_testing.py is checking for treq.collect when instead it should be treq.content

@glyph
Copy link
Member

glyph commented Jul 8, 2015

abortConnection's call to loseConnection: I guess abortConnection is never called - I'm not sure how to trigger that in the tests - will that work if we just cancel an outstanding request?

That should be what happens, yes.

@glyph
Copy link
Member

glyph commented Jul 8, 2015

IStringResponseStubs.get_response_for - I'd expect this to not be covered at all, since it's an interface - I'm not sure why it's partially covered. I guess we should add a # pragma no-cover or something to this line?

I think that a pragma is a reasonable choice for now.

cyli added 3 commits July 8, 2015 17:14
…d ensure that it only accepts bytes or unicode.
…add test for AbortableStringTransport.abortConnection
self.successResultOf(stub.content(resp)))


class SequenceStringStubsTests(TestCase):
Copy link
Member Author

Choose a reason for hiding this comment

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

Note to reviewers: I plan to write docs in a later PR, because don't want to make this PR already bigger than the monster it already is. But the API should look something like how these tests are written - maybe we can provide a convenience API like string_stub_treq([((expected request args), (expected response))]) instead of having to construct StubTreq(StringStubbingResource(SequenceStringStubs([...])))?

…erted in a future PR to add this functionality back in.
@cyli
Copy link
Member Author

cyli commented Jul 9, 2015

Note to reviewer - I removed some code to make this PR smaller to review. For reference, 75b49b1 is the code that was removed, and is what I intend to submit for the next PR (should give some idea of what the API for stubbing responses should look like)

@hynek
Copy link
Member

hynek commented Jul 9, 2015

Two things I wouldn’t know where else to mention:

  1. great to see that codecov already proved useful :)
  2. IMHO we should put all interfaces into a separate file (_interfaces.py?) and exclude the whole file in coverage instead of littering the code with pragmas

Conflicts:
	requirements-dev.txt
	setup.py

def test_providing_resource_to_stub_treq(self):
"""
The resource provided to StubTreq is responds to every request no
Copy link
Member

Choose a reason for hiding this comment

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

stray "is" here

@radix
Copy link
Member

radix commented Jul 9, 2015

Couple of things I noticed while tinkering around in my REPL:

  • if I request an invalid URL like "" or "abc", I get a synchronous IndexError. If I make a request like that with treq I get a Deferred with a SchemeNotSupported error.
  • I made a request against a blank Resource() to http://google.com/, and then if I make a request to "abc" or "" again, I get a synchronous AlreadyCalledError, which seems strange and could be indicative of a deeper bug.

@cyli
Copy link
Member Author

cyli commented Jul 12, 2015

@radix: thanks for finding that bug! Fixed it so it now bails out early if the response from the real agent already has a (failure) response

@hynek hynek mentioned this pull request Jul 13, 2015
@radix
Copy link
Member

radix commented Jul 13, 2015

I am a little surprised that making requests with https URLs without PyOpenSSL is giving me an "SSL support unavailable" error.

This LGTM!

@glyph
Copy link
Member

glyph commented Jul 13, 2015

Let's deal with the HTTPS support issue in a future version - that's a problem with treq itself, not this code :).

glyph added a commit that referenced this pull request Jul 13, 2015
Add an in-memory stub version of treq
@glyph glyph merged commit 6daab31 into master Jul 13, 2015
@cyli cyli deleted the fake-treq branch July 13, 2015 23:39
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.

None yet

5 participants