Commit 3fcc8133 authored by Jacek Furmankiewicz's avatar Jacek Furmankiewicz

added parsing of incoming YAML

parent 9fdea465
...@@ -33,11 +33,80 @@ Feature: Content types ...@@ -33,11 +33,80 @@ Feature: Content types
""" """
Then I expect HTTP code <code> Then I expect HTTP code <code>
# ElementTree object # ElementTree object
And I expect content contains '<Element 'root' at' And I expect content contains '<root><test>TEST</test><test2>Yo</test2></root>'
Examples: Examples:
| method | code | | method | code |
| POST | 201 | | POST | 201 |
| PUT | 200 | | PUT | 200 |
@yaml
Scenario Outline: Parse incoming YAML data
Given 'home_resource' is running
When as user 'None:None' I <method> 'http://127.0.0.1:8080/post/yaml' with YAML
"""
invoice: 34843
date : 2001-01-23
bill-to: &id001
given : Chris
family : Dumars
address:
lines: |
458 Walkman Dr.
Suite #292
city : Royal Oak
state : MI
postal : 48046
ship-to: *id001
product:
- sku : BL394D
quantity : 4
description : Basketball
price : 450.00
- sku : BL4438H
quantity : 1
description : Super Hoop
price : 2392.00
tax : 251.42
total: 4443.52
comments: >
Late afternoon is best.
Backup contact is Nancy
Billsmer @ 338-4338.
"""
Then I expect HTTP code <code>
And I expect content contains
"""
bill-to: &id001
address:
city: Royal Oak
lines: '458 Walkman Dr.
Suite #292
'
postal: 48046
state: MI
family: Dumars
given: Chris
comments: Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338.
date: 2001-01-23
invoice: 34843
product:
- description: Basketball
price: 450.0
quantity: 4
sku: BL394D
- description: Super Hoop
price: 2392.0
quantity: 1
sku: BL4438H
ship-to: *id001
tax: 251.42
total: 4443.52
"""
Examples:
| method | code |
| POST | 201 |
| PUT | 200 |
\ No newline at end of file
...@@ -6,7 +6,8 @@ Server tests ...@@ -6,7 +6,8 @@ Server tests
from corepost.web import CorePost, route from corepost.web import CorePost, route
from corepost.enums import Http from corepost.enums import Http
from twisted.internet import defer from twisted.internet import defer
import json from xml.etree import ElementTree
import json, yaml
class HomeApp(CorePost): class HomeApp(CorePost):
...@@ -47,7 +48,12 @@ class HomeApp(CorePost): ...@@ -47,7 +48,12 @@ class HomeApp(CorePost):
@route("/post/xml",(Http.POST,Http.PUT)) @route("/post/xml",(Http.POST,Http.PUT))
def test_xml(self,request,**kwargs): def test_xml(self,request,**kwargs):
return "%s" % request.xml return "%s" % ElementTree.tostring(request.xml)
@route("/post/yaml",(Http.POST,Http.PUT))
def test_yaml(self,request,**kwargs):
return "%s" % yaml.dump(request.yaml,indent=4,width=130,default_flow_style=False)
def run_app_home(): def run_app_home():
app = HomeApp() app = HomeApp()
......
...@@ -68,12 +68,17 @@ def when_as_user_i_send_post_put_to_url(user,password,method,url,params): ...@@ -68,12 +68,17 @@ def when_as_user_i_send_post_put_to_url(user,password,method,url,params):
scc.http_headers['Content-type'] = 'application/x-www-form-urlencoded' scc.http_headers['Content-type'] = 'application/x-www-form-urlencoded'
scc.response, scc.content = h.request(url, method, urlencode(as_dict(params)), headers = scc.http_headers) scc.response, scc.content = h.request(url, method, urlencode(as_dict(params)), headers = scc.http_headers)
@When(r"^as user '(.+):(.+)' I (POST|PUT) '(.+)' with (XML|JSON)\s*$") @When(r"^as user '(.+):(.+)' I (POST|PUT) '(.+)' with (XML|JSON|YAML)\s*$")
def when_as_user_i_send_post_put_xml_json_to_url(payload,user,password,method,url,request_type): def when_as_user_i_send_post_put_xml_json_to_url(payload,user,password,method,url,request_type):
h = httplib2.Http() h = httplib2.Http()
h.follow_redirects = False h.follow_redirects = False
h.add_credentials(user, password) h.add_credentials(user, password)
scc.http_headers['Content-type'] = 'application/json' if request_type == "JSON" else 'text/xml' if request_type == "JSON":
scc.http_headers['Content-type'] = 'application/json'
elif request_type == "XML":
scc.http_headers['Content-type'] = 'text/xml'
elif request_type == "YAML":
scc.http_headers['Content-type'] = 'text/yaml'
scc.response, scc.content = h.request(url, method, payload, headers = scc.http_headers) scc.response, scc.content = h.request(url, method, payload, headers = scc.http_headers)
@When("I prepare HTTP header '(.*)' = '(.*)'") @When("I prepare HTTP header '(.*)' = '(.*)'")
......
...@@ -12,9 +12,11 @@ from twisted.internet import reactor, defer ...@@ -12,9 +12,11 @@ from twisted.internet import reactor, defer
from twisted.web.http import parse_qs from twisted.web.http import parse_qs
from twisted.web.resource import Resource from twisted.web.resource import Resource
from twisted.web.server import Site, NOT_DONE_YET from twisted.web.server import Site, NOT_DONE_YET
import re, copy, exceptions, json import re, copy, exceptions, json, yaml
from xml.etree import ElementTree from xml.etree import ElementTree
class RequestRouter: class RequestRouter:
''' Common class for containing info related to routing a request to a function ''' ''' Common class for containing info related to routing a request to a function '''
...@@ -230,7 +232,7 @@ class CorePost(Resource): ...@@ -230,7 +232,7 @@ class CorePost(Resource):
except exceptions.TypeError as ex: except exceptions.TypeError as ex:
return self.__renderError(request,400,"%s" % ex) return self.__renderError(request,400,"%s" % ex)
except Exception as ex: except Exception as ex:
return self.__renderError(request,500,"Unexpected server error: %s" % type(ex)) return self.__renderError(request,500,"Unexpected server error: %s\n%s" % (type(ex),ex))
else: else:
return self.__renderError(request,404,"URL '%s' not found\n" % request.path) return self.__renderError(request,404,"URL '%s' not found\n" % request.path)
...@@ -250,8 +252,7 @@ class CorePost(Resource): ...@@ -250,8 +252,7 @@ class CorePost(Resource):
elif type in (MediaType.APPLICATION_XML,MediaType.TEXT_XML): elif type in (MediaType.APPLICATION_XML,MediaType.TEXT_XML):
request.xml = ElementTree.XML(request.content.read()) request.xml = ElementTree.XML(request.content.read())
elif type == MediaType.TEXT_YAML: elif type == MediaType.TEXT_YAML:
#TODO: parse YAML request.yaml = yaml.safe_load(request.content.read())
pass
def run(self,port=8080): def run(self,port=8080):
"""Shortcut for running app within Twisted reactor""" """Shortcut for running app within Twisted reactor"""
......
...@@ -83,6 +83,7 @@ setup( ...@@ -83,6 +83,7 @@ setup(
'httplib2>=0.7.1', 'httplib2>=0.7.1',
'freshen>=0.2', 'freshen>=0.2',
'formencode>=1.2.4', 'formencode>=1.2.4',
'pyyaml>=3.1.0',
], ],
) )
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment