'Test that a gzip Content-Encoded .gz file is gunzipped
only once by the middleware, leaving gunzipping of the file
to upper layers.'

def test_process_response_gzipped_gzip_file(self):

    headers = {'Content-Type': 'application/gzip', 'Content-Encoding': 'gzip'}
    f = BytesIO()
    plainbody = '<?xml    version="1.0"    encoding="UTF-8"?>\n<urlset    xmlns="http://www.google.com/schemas/sitemap/0.84">\n        <url>\n                <loc>http://www.example.com/</loc>\n                <lastmod>2009-08-16</lastmod>\n                <changefreq>daily</changefreq>\n                <priority>1</priority>\n        </url>\n        <url>\n                <loc>http://www.example.com/Special-Offers.html</loc>\n                <lastmod>2009-08-16</lastmod>\n                <changefreq>weekly</changefreq>\n                <priority>0.8</priority>\n        </url>\n</urlset>'
    gz_file = GzipFile(fileobj=f, mode='wb')
    gz_file.write(plainbody)
    gz_file.close()
    r = BytesIO()
    gz_resp = GzipFile(fileobj=r, mode='wb')
    gz_resp.write(f.getvalue())
    gz_resp.close()
    response = Response('http;//www.example.com/', headers=headers, body=r.getvalue())
    request = Request('http://www.example.com/')
    newresponse = self.mw.process_response(request, response, self.spider)
    self.assertEqual(gunzip(newresponse.body), plainbody)
