Skip to content
Browse files

Changed the keyword parameters

Added a three new tests for post, put and head requests
  • Loading branch information...
1 parent be7f156 commit 7137d1346275619f87e910671f17657a3df56e67 @bulkan committed Aug 31, 2011
Showing with 49 additions and 15 deletions.
  1. +20 −15 src/RequestsLibrary.py
  2. +29 −0 tests/testcase.txt
View
35 src/RequestsLibrary.py
@@ -1,14 +1,12 @@
import requests
-
-import urllib
-import urllib2
import json
+from urllib import urlencode
+
import robot
from robot.libraries.BuiltIn import BuiltIn
-
class RequestsLibrary(object):
ROBOT_LIBRARY_SCOPE = 'Global'
@@ -19,6 +17,9 @@ def __init__(self):
self._cache = robot.utils.ConnectionCache('No sessions created')
+ requests.settings.base_headers['User-Agent'] = 'robotframework-requests'
+
+
self.builtin = BuiltIn()
@@ -56,60 +57,64 @@ def to_json(self, content):
return json.loads(content)
- def get(self, alias, uri, **kwargs):
+ def get(self, alias, uri, headers=None):
''' Get: send a GET request on the session object found using the given alias
'''
session = self._cache.switch(alias)
- resp = session.get(uri, **kwargs)
+ resp = session.get(uri, headers=headers)
# store the last response object
session.last_resp = resp
return resp
- def post(self, alias, uri, **kwargs):
+ def post(self, alias, uri, data=None, headers=None):
''' Post: send a POST request on the session object found using the given alias
'''
+ if data:
+ data = urlencode(data)
+
session = self._cache.switch(alias)
- resp = session.post(uri, **kwargs)
+ resp = session.post(uri, data=data, headers=headers)
# store the last response object
session.last_resp = resp
+ self.builtin.log("Post response: " + resp.content, 'DEBUG')
return resp
- def put(self, alias, uri, **kwargs):
- ''' Put: send a GET request on the session object found using the given alias
+ def put(self, alias, uri, data=None, headers=None):
+ ''' Put: send a PUT request on the session object found using the given alias
'''
session = self._cache.switch(alias)
- resp = session.put(uri, **kwargs)
+ resp = session.put(uri, data=urlencode(data), headers=headers)
# store the last response object
session.last_resp = resp
return resp
- def delete(self, alias, uri, **kwargs):
+ def delete(self, alias, uri, headers=None):
''' Delete: send a DELETE request on the session object found using the given alias
'''
session = self._cache.switch(alias)
- resp = session.delete(uri, **kwargs)
+ resp = session.delete(uri, headers=headers)
# store the last response object
session.last_resp = resp
return resp
- def head(self, alias, uri, **kwargs):
+ def head(self, alias, uri, headers=None):
''' Delete: send a HEAD request on the session object found using the given alias
'''
session = self._cache.switch(alias)
- resp = session.head(uri, **kwargs)
+ resp = session.head(uri, headers=headers)
# store the last response object
session.last_resp = resp
View
29 tests/testcase.txt
@@ -26,3 +26,32 @@ Get With Auth
Should Be Equal As Strings ${resp.status_code} 200
${jsondata}= To Json ${resp.content}
Should Be Equal As Strings ${jsondata['authenticated']} True
+
+
+Post Requests
+ Create Session httpbin http://httpbin.org
+
+ ${data} Create Dictionary name bulkan surname evcimen
+ ${headers} Create Dictionary Content-Type application/x-www-form-urlencoded
+ ${resp} Post httpbin /post data=${data}
+ ${jsondata}= To Json ${resp.content}
+ Dictionary Should Contain Value ${jsondata['form']} bulkan
+ Dictionary Should Contain Value ${jsondata['form']} evcimen
+
+
+
+Put Requests
+ Create Session httpbin http://httpbin.org
+
+ ${data} Create Dictionary name bulkan surname evcimen
+ ${headers} Create Dictionary Content-Type application/x-www-form-urlencoded
+ ${resp} Put httpbin /put data=${data}
+ ${jsondata}= To Json ${resp.content}
+ Dictionary Should Contain Value ${jsondata['form']} bulkan
+ Dictionary Should Contain Value ${jsondata['form']} evcimen
+
+
+Head Request
+ Create Session httpbin http://httpbin.org
+ ${resp} Head httpbin /headers
+ Should Be Equal As Strings ${resp.status_code} 200

0 comments on commit 7137d13

Please sign in to comment.
Something went wrong with that request. Please try again.