def test_postprocess_response_headers_precise(self): ''' Test that a recursively similar Location: domain target is not recursively replaced ''' from mobilize.base import MobileSite, Domains domains = Domains(mobile='testwww.example.com:2280', desktop='www.example.com', production_http_desktop='www.example.com') msite = MobileSite(domains, []) headers = [ ('host' , 'testwww.example.com'), ('location' , 'http://testwww.example.com:2280/path/to/file'), ] modified = msite.postprocess_response_headers(headers, 302) location_values = list(v for k, v in modified if 'location' == k) # Should just find one match... assert len(location_values) == 1, len(location_values) # Make sure it's not http://testtestwww.example.com:2280:2280/path/to/file self.assertEqual('http://testwww.example.com:2280/path/to/file', location_values[0]) # again with different location headers = [ ('host' , 'testwww.example.com'), ('location' , 'http://testwww.example.com:2280'), ] modified = msite.postprocess_response_headers(headers, 302) location_values = list(v for k, v in modified if 'location' == k) assert len(location_values) == 1, len(location_values) self.assertEqual('http://testwww.example.com:2280', location_values[0])
def test_postprocess_response_headers(self): '''test that we rewrite the Location: header on 301 or 302 redirect, that we drop the transfer-encoding header, and other postprocessing of headers for all content (not just that which is mobilizeable''' from mobilize.base import MobileSite, Domains domains = Domains(mobile='m.example.com', desktop='www.example.com') msite = MobileSite(domains, []) # drop transfer-encoding header headers = [ ('transfer-encoding' , 'chunked'), ('host' , 'www.example.com'), ] modified = msite.postprocess_response_headers(headers, 200) modified_keys = set(k for k, v in modified) self.assertTrue('transfer-encoding' not in modified_keys) # rewrite location on 301 headers = [ ('host' , 'www.example.com'), ('location' , 'http://www.example.com/path/to/file'), ] modified = msite.postprocess_response_headers(headers, 301) location_values = list(v for k, v in modified if 'location' == k) # Should just find one match... assert len(location_values) == 1, len(location_values) self.assertEqual('http://m.example.com/path/to/file', location_values[0]) # same for 302 modified = msite.postprocess_response_headers(headers, 302) location_values = list(v for k, v in modified if 'location' == k) # Should just find one match... assert len(location_values) == 1, len(location_values) self.assertEqual('http://m.example.com/path/to/file', location_values[0])
def test_must_fake_http_head(self): from mobilize.base import MobileSite, Domains, HandlerMap class MockRequestInfo: method = None rel_url = None msite = MobileSite(Domains('m.example.com', 'example.com'), HandlerMap([])) reqinfo = MockRequestInfo() msite.fake_head_requests = True reqinfo.method = 'HEAD' reqinfo.rel_url = '/' self.assertTrue(msite.must_fake_http_head(reqinfo)) msite.fake_head_requests = False reqinfo.method = 'HEAD' reqinfo.rel_url = '/' self.assertFalse(msite.must_fake_http_head(reqinfo)) msite.fake_head_requests = True reqinfo.method = 'GET' reqinfo.rel_url = '/' self.assertFalse(msite.must_fake_http_head(reqinfo)) msite.fake_head_requests = True reqinfo.method = 'HEAD' reqinfo.rel_url = '/foo' self.assertFalse(msite.must_fake_http_head(reqinfo)) msite.fake_head_requests = True reqinfo.method = 'POST' reqinfo.rel_url = '/' self.assertFalse(msite.must_fake_http_head(reqinfo))