Skip to content

Commit

Permalink
attemp to fix travis fails
Browse files Browse the repository at this point in the history
  • Loading branch information
tpeng committed Nov 25, 2014
1 parent a69f042 commit cd19382
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
15 changes: 10 additions & 5 deletions tests/mockserver.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from __future__ import print_function
import sys, time, random, urllib, os, json
import six
from subprocess import Popen, PIPE
from twisted.web.server import Site, NOT_DONE_YET, GzipEncoderFactory
from twisted.web.resource import Resource, EncodingResourceWrapper
from twisted.web.server import Site, NOT_DONE_YET
from twisted.web.resource import Resource
from twisted.internet import reactor, defer, ssl
from twisted.web.test.test_webclient import PayloadResource
from scrapy import twisted_version


Expand Down Expand Up @@ -168,8 +168,13 @@ def __init__(self):
self.putChild("drop", Drop())
self.putChild("raw", Raw())
self.putChild("echo", Echo())
self.putChild('payload', PayloadResource())
self.putChild("xpayload", EncodingResourceWrapper(PayloadResource(), [GzipEncoderFactory()]))

if six.PY2 and twisted_version > (12, 3, 0):
from twisted.web.test.test_webclient import PayloadResource
from twisted.web.server import GzipEncoderFactory
from twisted.web.resource import EncodingResourceWrapper
self.putChild('payload', PayloadResource())
self.putChild("xpayload", EncodingResourceWrapper(PayloadResource(), [GzipEncoderFactory()]))

def getChild(self, name, request):
return self
Expand Down
38 changes: 22 additions & 16 deletions tests/test_downloader_handlers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import twisted
import six

from twisted.trial import unittest
from twisted.protocols.policies import WrappingFactory
Expand Down Expand Up @@ -287,23 +288,28 @@ def test_download(self):

@defer.inlineCallbacks
def test_download_gzip_response(self):
crawler = get_crawler(SingleRequestSpider)
body = '1'*100 # PayloadResource requires body length to be 100
request = Request('http://localhost:8998/payload', method='POST', body=body, meta={'download_maxsize': 50})
yield crawler.crawl(seed=request)
failure = crawler.spider.meta['failure']
# download_maxsize < 100, hence the CancelledError
self.assertIsInstance(failure.value, defer.CancelledError)

request.headers.setdefault('Accept-Encoding', 'gzip,deflate')
request = request.replace(url='http://localhost:8998/xpayload')
yield crawler.crawl(seed=request)

# download_maxsize = 50 is enough for the gzipped response
failure = crawler.spider.meta.get('failure')
self.assertTrue(failure == None)
reason = crawler.spider.meta['close_reason']
self.assertTrue(reason, 'finished')
if six.PY2 and twisted_version > (12, 3, 0):

crawler = get_crawler(SingleRequestSpider)
body = '1'*100 # PayloadResource requires body length to be 100
request = Request('http://localhost:8998/payload', method='POST', body=body, meta={'download_maxsize': 50})
yield crawler.crawl(seed=request)
failure = crawler.spider.meta['failure']
# download_maxsize < 100, hence the CancelledError
self.assertIsInstance(failure.value, defer.CancelledError)

request.headers.setdefault('Accept-Encoding', 'gzip,deflate')
request = request.replace(url='http://localhost:8998/xpayload')
yield crawler.crawl(seed=request)

# download_maxsize = 50 is enough for the gzipped response
failure = crawler.spider.meta.get('failure')
self.assertTrue(failure == None)
reason = crawler.spider.meta['close_reason']
self.assertTrue(reason, 'finished')
else:
raise unittest.SkipTest("xpayload and payload endpoint only enabled for twisted > 12.3.0 and python 2.x")


class UriResource(resource.Resource):
Expand Down

0 comments on commit cd19382

Please sign in to comment.