class TestTileClientOnError(object): def setup(self): self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.client = TMSClient(TESTSERVER_URL) def test_cacheable_response(self): error_handler = HTTPSourceErrorHandler() error_handler.add_handler(500, (255, 0, 0), cacheable=True) self.source = TiledSource(self.grid, self.client, error_handler=error_handler) with mock_httpd( TEST_SERVER_ADDRESS, [({"path": "/1/0/0.png"}, {"body": b"error", "status": 500, "headers": {"content-type": "text/plain"}})], ): resp = self.source.get_map(MapQuery([-180, -90, 0, 90], (256, 256), SRS(4326), format="png")) assert resp.cacheable eq_(resp.as_image().getcolors(), [((256 * 256), (255, 0, 0))]) def test_image_response(self): error_handler = HTTPSourceErrorHandler() error_handler.add_handler(500, (255, 0, 0), cacheable=False) self.source = TiledSource(self.grid, self.client, error_handler=error_handler) with mock_httpd( TEST_SERVER_ADDRESS, [({"path": "/1/0/0.png"}, {"body": b"error", "status": 500, "headers": {"content-type": "text/plain"}})], ): resp = self.source.get_map(MapQuery([-180, -90, 0, 90], (256, 256), SRS(4326), format="png")) assert not resp.cacheable eq_(resp.as_image().getcolors(), [((256 * 256), (255, 0, 0))]) def test_multiple_image_responses(self): error_handler = HTTPSourceErrorHandler() error_handler.add_handler(500, (255, 0, 0), cacheable=False) error_handler.add_handler(204, (255, 0, 127, 200), cacheable=True) self.source = TiledSource(self.grid, self.client, error_handler=error_handler) with mock_httpd( TEST_SERVER_ADDRESS, [ ({"path": "/1/0/0.png"}, {"body": b"error", "status": 500, "headers": {"content-type": "text/plain"}}), ({"path": "/1/0/0.png"}, {"body": b"error", "status": 204, "headers": {"content-type": "text/plain"}}), ], ): resp = self.source.get_map(MapQuery([-180, -90, 0, 90], (256, 256), SRS(4326), format="png")) assert not resp.cacheable eq_(resp.as_image().getcolors(), [((256 * 256), (255, 0, 0))]) resp = self.source.get_map(MapQuery([-180, -90, 0, 90], (256, 256), SRS(4326), format="png")) assert resp.cacheable eq_(resp.as_image().getcolors(), [((256 * 256), (255, 0, 127, 200))])
class TestTileClientOnError(object): def setup(self): self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.client = TileClient(TileURLTemplate(TESTSERVER_URL)) def test_cacheable_response(self): error_handler = HTTPSourceErrorHandler() error_handler.add_handler(500, (255, 0, 0), cacheable=True) self.source = TiledSource(self.grid, self.client, error_handler=error_handler) with mock_httpd(TEST_SERVER_ADDRESS, [({'path': '/1/0/0.png'}, {'body': b'error', 'status': 500, 'headers':{'content-type': 'text/plain'}})]): resp = self.source.get_map(MapQuery([-180, -90, 0, 90], (256, 256), SRS(4326), format='png')) assert resp.cacheable eq_(resp.as_image().getcolors(), [((256*256), (255, 0, 0))]) def test_image_response(self): error_handler = HTTPSourceErrorHandler() error_handler.add_handler(500, (255, 0, 0), cacheable=False) self.source = TiledSource(self.grid, self.client, error_handler=error_handler) with mock_httpd(TEST_SERVER_ADDRESS, [({'path': '/1/0/0.png'}, {'body': b'error', 'status': 500, 'headers':{'content-type': 'text/plain'}})]): resp = self.source.get_map(MapQuery([-180, -90, 0, 90], (256, 256), SRS(4326), format='png')) assert not resp.cacheable eq_(resp.as_image().getcolors(), [((256*256), (255, 0, 0))]) def test_multiple_image_responses(self): error_handler = HTTPSourceErrorHandler() error_handler.add_handler(500, (255, 0, 0), cacheable=False) error_handler.add_handler(204, (255, 0, 127, 200), cacheable=True) self.source = TiledSource(self.grid, self.client, error_handler=error_handler) with mock_httpd(TEST_SERVER_ADDRESS, [ ({'path': '/1/0/0.png'}, {'body': b'error', 'status': 500, 'headers':{'content-type': 'text/plain'}}), ({'path': '/1/0/0.png'}, {'body': b'error', 'status': 204, 'headers':{'content-type': 'text/plain'}})]): resp = self.source.get_map(MapQuery([-180, -90, 0, 90], (256, 256), SRS(4326), format='png')) assert not resp.cacheable eq_(resp.as_image().getcolors(), [((256*256), (255, 0, 0))]) resp = self.source.get_map(MapQuery([-180, -90, 0, 90], (256, 256), SRS(4326), format='png')) assert resp.cacheable eq_(resp.as_image().getcolors(), [((256*256), (255, 0, 127, 200))])
class TestTiledSourceGlobalGeodetic(object): def setup(self): self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.client = MockTileClient() self.source = TiledSource(self.grid, self.client) def test_match(self): self.source.get_map(MapQuery([-180, -90, 0, 90], (256, 256), SRS(4326))) self.source.get_map(MapQuery([0, -90, 180, 90], (256, 256), SRS(4326))) eq_(self.client.requested_tiles, [(0, 0, 1), (1, 0, 1)]) @raises(InvalidSourceQuery) def test_wrong_size(self): self.source.get_map(MapQuery([-180, -90, 0, 90], (512, 256), SRS(4326))) @raises(InvalidSourceQuery) def test_wrong_srs(self): self.source.get_map(MapQuery([-180, -90, 0, 90], (512, 256), SRS(4326)))
class TestTiledSourceGlobalGeodetic(object): def setup(self): self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.client = MockTileClient() self.source = TiledSource(self.grid, self.client) def test_match(self): self.source.get_map(MapQuery([-180, -90, 0, 90], (256, 256), SRS(4326))) self.source.get_map(MapQuery([0, -90, 180, 90], (256, 256), SRS(4326))) assert self.client.requested_tiles == [(0, 0, 1), (1, 0, 1)] def test_wrong_size(self): with pytest.raises(InvalidSourceQuery): self.source.get_map(MapQuery([-180, -90, 0, 90], (512, 256), SRS(4326))) def test_wrong_srs(self): with pytest.raises(InvalidSourceQuery): self.source.get_map(MapQuery([-180, -90, 0, 90], (512, 256), SRS(4326)))
class TestTileClientOnError(object): def setup(self): self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.client = TileClient(TileURLTemplate(TESTSERVER_URL)) def test_cacheable_response(self): error_handler = HTTPSourceErrorHandler() error_handler.add_handler(500, (255, 0, 0), cacheable=True) self.source = TiledSource(self.grid, self.client, error_handler=error_handler) with mock_httpd( TEST_SERVER_ADDRESS, [( { "path": "/1/0/0.png" }, { "body": b"error", "status": 500, "headers": { "content-type": "text/plain" }, }, )], ): resp = self.source.get_map( MapQuery([-180, -90, 0, 90], (256, 256), SRS(4326), format="png")) assert resp.cacheable assert resp.as_image().getcolors() == [((256 * 256), (255, 0, 0))] def test_image_response(self): error_handler = HTTPSourceErrorHandler() error_handler.add_handler(500, (255, 0, 0), cacheable=False) self.source = TiledSource(self.grid, self.client, error_handler=error_handler) with mock_httpd( TEST_SERVER_ADDRESS, [( { "path": "/1/0/0.png" }, { "body": b"error", "status": 500, "headers": { "content-type": "text/plain" }, }, )], ): resp = self.source.get_map( MapQuery([-180, -90, 0, 90], (256, 256), SRS(4326), format="png")) assert not resp.cacheable assert resp.as_image().getcolors() == [((256 * 256), (255, 0, 0))] def test_multiple_image_responses(self): error_handler = HTTPSourceErrorHandler() error_handler.add_handler(500, (255, 0, 0), cacheable=False) error_handler.add_handler(204, (255, 0, 127, 200), cacheable=True) self.source = TiledSource(self.grid, self.client, error_handler=error_handler) with mock_httpd( TEST_SERVER_ADDRESS, [ ( { "path": "/1/0/0.png" }, { "body": b"error", "status": 500, "headers": { "content-type": "text/plain" }, }, ), ( { "path": "/1/0/0.png" }, { "body": b"error", "status": 204, "headers": { "content-type": "text/plain" }, }, ), ], ): resp = self.source.get_map( MapQuery([-180, -90, 0, 90], (256, 256), SRS(4326), format="png")) assert not resp.cacheable assert resp.as_image().getcolors() == [((256 * 256), (255, 0, 0))] resp = self.source.get_map( MapQuery([-180, -90, 0, 90], (256, 256), SRS(4326), format="png")) assert resp.cacheable assert resp.as_image().getcolors() == [((256 * 256), (255, 0, 127, 200))]