def test_expects_w_header(self): serv = MockServ() serv.expects('/test', headers={'Accept': 'Coffee'}) with serv: resp = requests.get('http://localhost:%d/test' % serv.port, headers={'Accept': 'Coffee'}) assert resp.ok
def test_get_featureinfo_limited_to_inside(self): def auth(service, layers, query_extent, **kw): eq_(query_extent, ('EPSG:4326', (-80.0, -40.0, 0.0, 0.0))) eq_(service, 'wms.featureinfo') eq_(len(layers), 1) return { 'authorized': 'partial', 'layers': { 'layer1b': { 'featureinfo': True, 'limited_to': { 'srs': 'EPSG:4326', 'geometry': [-80.0, -40.0, 0.0, 0.0] } }, } } serv = MockServ(port=42423) serv.expects( '/service?request=GetFeatureInfo&service=WMS&Version=1.1.1&SRS=EPSG:4326' '&BBOX=-80.0,-40.0,0.0,0.0&WIDTH=200&HEIGHT=100&styles=&FORMAT=image/png&X=10&Y=10' '&query_layers=fi&layers=fi') serv.returns(b'infoinfo') with serv: resp = self.app.get(FI_REQ + 'query_layers=layer1b&layers=layer1b', extra_environ={'mapproxy.authorize': auth}) eq_(resp.body, b'infoinfo')
def test_expects_get_no_body(self): serv = MockServ() serv.expects('/test') with serv: resp = requests.get('http://localhost:%d/test' % serv.port) assert resp.status_code == 200 assert resp.content == b''
def test_get_tile_cascaded_cache(self): serv = MockServ(42423, bbox_aware_query_comparator=True, unordered=True) # gk3 cache requests UTM tiles serv.expects( '/service?layers=foo,bar&width=256&version=1.1.1&bbox=423495.931784,5596775.88732,501767.448748,5675047.40429&service=WMS&format=image%2Fpng&styles=&srs=EPSG%3A25832&request=GetMap&height=256' ).returns(TEST_TILE) serv.expects( '/service?layers=foo,bar&width=256&version=1.1.1&bbox=345224.41482,5596775.88732,423495.931784,5675047.40429&service=WMS&format=image%2Fpng&styles=&srs=EPSG%3A25832&request=GetMap&height=256' ).returns(TEST_TILE) serv.expects( '/service?layers=foo,bar&width=256&version=1.1.1&bbox=345224.41482,5518504.37036,423495.931784,5596775.88732&service=WMS&format=image%2Fpng&styles=&srs=EPSG%3A25832&request=GetMap&height=256' ).returns(TEST_TILE) serv.expects( '/service?layers=foo,bar&width=256&version=1.1.1&bbox=423495.931784,5518504.37036,501767.448748,5596775.88732&service=WMS&format=image%2Fpng&styles=&srs=EPSG%3A25832&request=GetMap&height=256' ).returns(TEST_TILE) serv.expects( '/service?layers=foo,bar&width=256&version=1.1.1&bbox=345224.41482,5440232.8534,423495.931784,5518504.37036&service=WMS&format=image%2Fpng&styles=&srs=EPSG%3A25832&request=GetMap&height=256' ).returns(TEST_TILE) serv.expects( '/service?layers=foo,bar&width=256&version=1.1.1&bbox=423495.931784,5440232.8534,501767.448748,5518504.37036&service=WMS&format=image%2Fpng&styles=&srs=EPSG%3A25832&request=GetMap&height=256' ).returns(TEST_TILE) self.common_tile_req.params['tilematrixset'] = 'gk3' with serv: resp = self.app.get(str(self.common_tile_req)) eq_(resp.content_type, 'image/png') data = BytesIO(resp.body) assert is_png(data)
def check_get_tile_limited_to(self, auth_dict): def auth(service, layers, environ, query_extent, **kw): eq_(environ['PATH_INFO'], '/wmts/layer3/GLOBAL_MERCATOR/1/0/0.jpeg') eq_(service, 'wmts') eq_(len(layers), 1) eq_(query_extent[0], 'EPSG:900913') assert bbox_equals(query_extent[1], (-20037508.342789244, 0, 0, 20037508.342789244)) return auth_dict serv = MockServ(port=42423) serv.expects('/1/0/1.png') serv.returns(create_tmp_image((256, 256), color=(255, 0, 0)), headers={'content-type': 'image/png'}) with serv: resp = self.app.get('/wmts/layer3/GLOBAL_MERCATOR/1/0/0.jpeg', extra_environ={'mapproxy.authorize': auth}) eq_(resp.content_type, 'image/png') img = img_from_buf(resp.body) img = img.convert('RGBA') # left part authorized, red eq_( img.crop((0, 0, 127, 255)).getcolors()[0], (127 * 255, (255, 0, 0, 255))) # right part not authorized, transparent eq_(img.crop((129, 0, 255, 255)).getcolors()[0][1][3], 0)
def test_get_tile_limited_to_inside(self): def auth(service, layers, environ, **kw): eq_(environ['PATH_INFO'], '/wmts/layer3/GLOBAL_MERCATOR/1/0/0.jpeg') eq_(service, 'wmts') eq_(len(layers), 1) return { 'authorized': 'partial', 'limited_to': { 'geometry': [-180, -89, 180, 89], 'srs': 'EPSG:4326', }, 'layers': { 'layer3': { 'tile': True }, } } serv = MockServ(port=42423) serv.expects('/1/0/1.png') serv.returns(create_tmp_image((256, 256), color=(255, 0, 0)), headers={'content-type': 'image/png'}) with serv: resp = self.app.get('/wmts/layer3/GLOBAL_MERCATOR/1/0/0.jpeg', extra_environ={'mapproxy.authorize': auth}) eq_(resp.content_type, 'image/jpeg') img = img_from_buf(resp.body) eq_(img.getcolors()[0], (256 * 256, (255, 0, 0)))
def test_get_map_uncached(self): mbtiles_file = os.path.join(test_config['base_dir'], 'cache.mbtiles') tiles_lock_dir = os.path.join(test_config['base_dir'], 'testlockdir') assert os.path.exists(mbtiles_file) # already created on startup assert not os.path.exists(tiles_lock_dir) self.common_map_req.params.bbox = '-180,0,0,80' serv = MockServ(port=42423) serv.expects('/tiles/01/000/000/000/000/000/001.png') serv.returns(create_tmp_image((256, 256))) with serv: resp = self.app.get(self.common_map_req) eq_(resp.content_type, 'image/png') data = StringIO(resp.body) assert is_png(data) # now cached resp = self.app.get(self.common_map_req) eq_(resp.content_type, 'image/png') data = StringIO(resp.body) assert is_png(data) # custom tile_lock_dir created assert os.path.exists(tiles_lock_dir)
def test_expects_post(self): # TODO POST handling in MockServ is hacky. # data just gets appended to URL serv = MockServ() serv.expects('/test?foo', method='POST') with serv: requests.post('http://localhost:%d/test' % serv.port, data=b'foo')
def test_get_tile_flipped_axis(self): serv = MockServ(port=42423) # source is ll, cache/service ul serv.expects('/tiles/01/000/000/000/000/000/001.png') serv.returns(create_tmp_image((256, 256))) with serv: resp = self.app.get('/wmts/myrest/tms_cache_ul/ulgrid/01/0/0.png', status=200) eq_(resp.content_type, 'image/png')
def test_get_tile_kvp_valid_dimension_defaults(self): serv = MockServ(42423, bbox_aware_query_comparator=True) serv.expects(DIMENSION_LAYER_BASE_REQ + '&Time=2012-11-15T00:00:00&elevation=0').returns( TEST_TILE) with serv: resp = self.app.get(WMTS_KVP_URL + '&layer=dimension_layer') eq_(resp.content_type, 'image/png')
def test_expects_w_header_but_missing(self): serv = MockServ() serv.expects('/test', headers={'Accept': 'Coffee'}) try: with serv: requests.get('http://localhost:%d/test' % serv.port) except RequestsMismatchError as ex: assert ex.assertions[0].expected == 'Accept: Coffee'
def test_returns_headers(self): serv = MockServ() serv.expects('/test') serv.returns(body=b'hello', headers={'content-type': 'text/plain'}) with serv: resp = requests.get('http://localhost:%d/test' % serv.port) assert resp.headers['Content-type'] == 'text/plain' assert resp.content == b'hello'
def test_get_tile_default_no_dimension_source(self): # check if dimensions are ignored serv = MockServ(42423, bbox_aware_query_comparator=True) serv.expects(NO_DIMENSION_LAYER_BASE_REQ).returns(TEST_TILE) with serv: resp = self.app.get( '/wmts/no_dimension_layer/GLOBAL_MERCATOR/default/default/01/0/0.png' ) eq_(resp.content_type, 'image/png')
def test_get_tile_webmerc(self): serv = MockServ(42423, bbox_aware_query_comparator=True) serv.expects( '/service?layers=foo,bar&width=256&version=1.1.1&bbox=-20037508.3428,0.0,0.0,20037508.3428&service=WMS&format=image%2Fpng&styles=&srs=EPSG%3A3857&request=GetMap&height=256').returns(TEST_TILE) with serv: resp = self.app.get(str(self.common_tile_req)) eq_(resp.content_type, 'image/png') data = BytesIO(resp.body) assert is_png(data)
def test_returns(self): serv = MockServ() serv.expects('/test') serv.returns(body=b'hello') with serv: resp = requests.get('http://localhost:%d/test' % serv.port) assert 'Content-type' not in resp.headers assert resp.content == b'hello'
def test_returns_status(self): serv = MockServ() serv.expects('/test') serv.returns(body=b'hello', status_code=418) with serv: resp = requests.get('http://localhost:%d/test' % serv.port) assert resp.status_code == 418 assert resp.content == b'hello'
def test_multiple_requests(self): serv = MockServ() serv.expects('/test1').returns(body=b'hello1') serv.expects('/test2').returns(body=b'hello2') with serv: resp = requests.get('http://localhost:%d/test1' % serv.port) assert resp.content == b'hello1' resp = requests.get('http://localhost:%d/test2' % serv.port) assert resp.content == b'hello2'
def test_get_tile_valid_dimension(self): serv = MockServ(42423) serv.expects(DIMENSION_LAYER_BASE_REQ + '&Time=2012-11-15T00:00:00&elevation=1000').returns( TEST_TILE) with serv: resp = self.app.get( '/wmts/dimension_layer/GLOBAL_MERCATOR/2012-11-15T00:00:00/1000/01/0/0.png' ) eq_(resp.content_type, 'image/png')
def test_get_tile_kvp_default_no_dimension_source(self): # check if dimensions are ignored serv = MockServ(42423, bbox_aware_query_comparator=True) serv.expects(NO_DIMENSION_LAYER_BASE_REQ).returns(TEST_TILE) with serv: resp = self.app.get( WMTS_KVP_URL + '&layer=no_dimension_layer&Time=2012-11-14T00:00:00&Elevation=3000' ) eq_(resp.content_type, 'image/png')
def test_get_tile_default_dimension(self): serv = MockServ(42423, bbox_aware_query_comparator=True) serv.expects(DIMENSION_LAYER_BASE_REQ + '&Time=2012-11-15T00:00:00&elevation=0').returns( TEST_TILE) with serv: resp = self.app.get( '/wmts/dimension_layer/GLOBAL_MERCATOR/default/default/01/0/0.png' ) eq_(resp.content_type, 'image/png')
def test_get_tile_valid_dimension(self, app): serv = MockServ(42423, bbox_aware_query_comparator=True) serv.expects( DIMENSION_LAYER_BASE_REQ + "&Time=2012-11-15T00:00:00&elevation=1000" ).returns(TEST_TILE) with serv: resp = app.get( "/wmts/dimension_layer/GLOBAL_MERCATOR/2012-11-15T00:00:00/1000/01/0/0.png" ) assert resp.content_type == "image/png"
def test_get_tile_utm(self): serv = MockServ(42423, bbox_aware_query_comparator=True) serv.expects( '/service?layers=foo,bar&width=256&version=1.1.1&bbox=-46133.17,5675047.40429,580038.965712,6301219.54&service=WMS&format=image%2Fpng&styles=&srs=EPSG%3A25832&request=GetMap&height=256').returns(TEST_TILE) self.common_tile_req.params['tilematrixset'] = 'utm32' with serv: resp = self.app.get(str(self.common_tile_req)) eq_(resp.content_type, 'image/png') data = BytesIO(resp.body) assert is_png(data)
def test_get_tile_kvp_valid_dimension(self): serv = MockServ(42423) serv.expects(DIMENSION_LAYER_BASE_REQ + '&Time=2012-11-14T00:00:00&elevation=3000').returns( TEST_TILE) with serv: resp = self.app.get( WMTS_KVP_URL + '&layer=dimension_layer&timE=2012-11-14T00:00:00&ELEvatioN=3000' ) eq_(resp.content_type, 'image/png')
def test_get_tile_kvp_valid_dimension(self, app): serv = MockServ(42423, bbox_aware_query_comparator=True) serv.expects( DIMENSION_LAYER_BASE_REQ + "&Time=2012-11-14T00:00:00&elevation=3000" ).returns(TEST_TILE) with serv: resp = app.get( WMTS_KVP_URL + "&layer=dimension_layer&timE=2012-11-14T00:00:00&ELEvatioN=3000" ) assert resp.content_type == "image/png"
def test_expects_post_but_get(self): serv = MockServ() serv.expects('/test', method='POST') try: with serv: requests.get('http://localhost:%d/test' % serv.port) except RequestsMismatchError as ex: assert ex.assertions[0].expected == 'POST' assert ex.assertions[0].actual == 'GET' else: raise AssertionError('AssertionError expected')
def test_get_tile_flipped_axis(self): self.common_tile_req.params['layer'] = 'tms_cache_ul' self.common_tile_req.params['tilematrixset'] = 'ulgrid' self.common_tile_req.params['format'] = 'image/png' self.common_tile_req.tile = (0, 0, '01') serv = MockServ(port=42423) # source is ll, cache/service ul serv.expects('/tiles/01/000/000/000/000/000/001.png') serv.returns(create_tmp_image((256, 256))) with serv: resp = self.app.get(str(self.common_tile_req), status=200) eq_(resp.content_type, 'image/png')
def test_get_tile_flipped_axis(self, app): serv = MockServ(port=42423) # source is ll, cache/service ul serv.expects("/tiles/01/000/000/000/000/000/001.png") serv.returns(create_tmp_image((256, 256))) with serv: resp = app.get("/wmts/myrest/tms_cache_ul/ulgrid/01/0/0.png", status=200) assert resp.content_type == "image/png" # test without leading 0 in level resp = app.get("/wmts/myrest/tms_cache_ul/ulgrid/1/0/0.png", status=200) assert resp.content_type == "image/png"
def test_too_many_requests(self): serv = MockServ() serv.expects('/test1').returns(body=b'hello1') with serv: resp = requests.get('http://localhost:%d/test1' % serv.port) assert resp.content == b'hello1' try: requests.get('http://localhost:%d/test2' % serv.port) except requests.exceptions.RequestException: pass else: raise AssertionError('RequestException expected')
def test_missing_requests(self): serv = MockServ() serv.expects('/test1').returns(body=b'hello1') serv.expects('/test2').returns(body=b'hello2') try: with serv: resp = requests.get('http://localhost:%d/test1' % serv.port) assert resp.content == b'hello1' except RequestsMismatchError as ex: assert 'requests mismatch:\n - missing requests' in str(ex) else: raise AssertionError('AssertionError expected')
def test_getfeatureinfo(self, app, fi_req): serv = MockServ(port=42423) serv.expects( "/service?layers=foo,bar" + "&bbox=-20037508.342789244,-20037508.342789244,20037508.342789244,20037508.342789244" + "&width=256&height=256&x=17&y=27&query_layers=foo,bar&format=image%2Fpng&srs=EPSG%3A900913" + "&request=GetFeatureInfo&version=1.1.1&service=WMS&styles=&info_format=application/json" ) serv.returns(b'{"data": 43}') with serv: resp = app.get(str(fi_req), status=200) assert resp.content_type == "application/json"