diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..3891fd0fa2416ca90e96b116ddeabe1d62187842 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python2.7 +# -*- coding: utf-8 -*- + +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # +# # +# MapServer REST API is a python wrapper around MapServer which # +# allows to manipulate a mapfile in a RESTFul way. It has been # +# developped to match as close as possible the way the GeoServer # +# REST API acts. # +# # +# Copyright (C) 2011-2013 Neogeo Technologies. # +# # +# This file is part of MapServer Rest API. # +# # +# MapServer Rest API is free software: you can redistribute it # +# and/or modify it under the terms of the GNU General Public License # +# as published by the Free Software Foundation, either version 3 of # +# the License, or (at your option) any later version. # +# # +# MapServer Rest API is distributed in the hope that it will be # +# useful, but WITHOUT ANY WARRANTY; without even the implied warranty # +# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + diff --git a/tests/files/timezones_shp.zip b/tests/files/timezones_shp.zip new file mode 100644 index 0000000000000000000000000000000000000000..cc6088fd910b73b1b580a26d116d882c42cdcff2 Binary files /dev/null and b/tests/files/timezones_shp.zip differ diff --git a/tests/testScenario.py b/tests/testScenario.py new file mode 100644 index 0000000000000000000000000000000000000000..70aa96a340fdcfb5e7da1726b76feb41fc5aec54 --- /dev/null +++ b/tests/testScenario.py @@ -0,0 +1,91 @@ +#!/usr/bin/env python2.7 +# -*- coding: utf-8 -*- + +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # +# # +# MapServer REST API is a python wrapper around MapServer which # +# allows to manipulate a mapfile in a RESTFul way. It has been # +# developped to match as close as possible the way the GeoServer # +# REST API acts. # +# # +# Copyright (C) 2011-2013 Neogeo Technologies. # +# # +# This file is part of MapServer Rest API. # +# # +# MapServer Rest API is free software: you can redistribute it # +# and/or modify it under the terms of the GNU General Public License # +# as published by the Free Software Foundation, either version 3 of # +# the License, or (at your option) any later version. # +# # +# MapServer Rest API is distributed in the hope that it will be # +# useful, but WITHOUT ANY WARRANTY; without even the implied warranty # +# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + +from utils import APIRequest + +import sys + +def testScenario(): + + target = "http://192.168.1.39/mra/" + map_name = "test" + + + # GET workspaces. + + wss = APIRequest("GET", target + "/maps/test/workspaces")["workspaces"] + assert len(wss) == 1 + assert wss[0]["name"] == "default" + + # GET workspaceName + + ws = APIRequest("GET", wss[0]["href"])["workspace"] + assert ws["name"] == wss[0]["name"] + + # GET dataStores + + dss = APIRequest("GET", ws["dataStores"]["href"])["dataStores"] + + # DELETE the dataStores + + for ds in dss: + APIRequest("DELETE", ds["href"]) + + # POST a datastore and GET it + + name, title = "testDS1", "test datastore 1" + _, r = APIRequest("POST", ws["dataStores"]["href"], {"dataStore":{"name":name, "title":title}}, + get_response=True) + ds_link = r.getheader("Location") + + ds = APIRequest("GET", ds_link)["dataStore"] + assert ds["name"] == name + assert ds["title"] == title + + # PUT a datastore + + ds["title"] = title.upper() + del ds["href"] + APIRequest("PUT", ds_link, {"dataStore":ds}) + + ds = APIRequest("GET", ds_link)["dataStore"] + assert ds["title"] == title.upper() + + # GET featuretypes + + fts = APIRequest("GET", ds["href"])["featureTypes"] + assert len(fts) == 0 + + + # PUT file + + APIRequest("PUT", ds_link + "/file.shp", open("./files/timezones_shp.zip", "rb"), + encode=None, content_type="application/zip") + + ds = APIRequest("GET", ds_link)["dataStore"] + assert ds["connectionParameters"]["path"] = "workspaces/%s/datastores/%s/timezones.shp" % ( + ws["name"], ds["name"]) + diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..1b0adba1f443674360071ea7302923d8a309b305 --- /dev/null +++ b/tests/utils.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python2.7 +# -*- coding: utf-8 -*- + +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # +# # +# MapServer REST API is a python wrapper around MapServer which # +# allows to manipulate a mapfile in a RESTFul way. It has been # +# developped to match as close as possible the way the GeoServer # +# REST API acts. # +# # +# Copyright (C) 2011-2013 Neogeo Technologies. # +# # +# This file is part of MapServer Rest API. # +# # +# MapServer Rest API is free software: you can redistribute it # +# and/or modify it under the terms of the GNU General Public License # +# as published by the Free Software Foundation, either version 3 of # +# the License, or (at your option) any later version. # +# # +# MapServer Rest API is distributed in the hope that it will be # +# useful, but WITHOUT ANY WARRANTY; without even the implied warranty # +# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + +import httplib + +import pyxml +import json + +import sys + +def deduce_content_type(type): + if type == "json": + return "application/json" + elif type == "xml": + return "application/xml" + + +def APIRequest(method, url, data=None, encode="json", decode="json", content_type=None, expected_type=None, get_response=False): + + if encode == "json": + data = json.dumps(data) + elif encode == "xml": + data = pyxml.dumps(data) + + if content_type == None: + content_type = deduce_content_type(encode) + + surl = httplib.urlsplit(url) + + if encode and not url.endswith("." + encode): + url = surl.path + "." + encode + + print >>sys.stderr, method, url + conn = httplib.HTTPConnection(surl.hostname, surl.port) + conn.request(method, url, body=data, headers={"Content-Type":content_type}) + + r = conn.getresponse() + + if expected_type == None: + expected_type = deduce_content_type(decode) + + # assert expected_type in r.getheader("Content-Type"), "received %s instead of %s" % ( + # r.getheader("Content-Type"), expected_type) + + recv = r.read() + + try: + if decode == "json": + recv = json.loads(recv) + elif decode == "xml": + recv = pyxml.loads(recv) + except: + pass + + print >>sys.stderr, r.status, r.reason + assert 200 <= r.status < 300, recv + + return (recv, r) if get_response else recv + +