Testing REST API with restclient


Here are some code snippets from the test that I wrote for testing the REST API for one of our projects.
I am using appengine-rest-server as API provider on the server.

Do not copy these example verbatim, I’m only showing them as examples.
Each of this methods is defined inside of a class

Imports


from nose.tools import assert_equals, assert_true, assert_false
from restclient import GET, POST, PUT, DELETE
from lxml import etree, objectify
from StringIO import StringIO

Verifying XML against XML Schema


    def verify_against_schema(self, xml, schema):
        '''
        Check if data is valid for provided schema

        xml data to verify
        schema is url to the xml schema
        '''
        schema = etree.XMLSchema(etree.parse(StringIO(schema)))
        parser = etree.XMLParser(schema=schema)
        return etree.parse(StringIO(xml), parser=parser)

    def test_review_schema(self):
        'Vacation :: Verifying review schema'

        xml = '''
        
            4
            4
            Wedding
            4
            34r3423f3rf32f3f
            334323443
            300
            true
            2009-10-07
            Family
            4
        
        '''

        verified = self.verify_against_schema(xml, GET(self.url('/api/rest/metadata/Review')))
        assert_true(verified)

Creating an item



    def create_review(self, resort, full=False):
        'Create review for testing purposes'

        xml = '''
        
            4
            4
            Wedding
            4
            %s
            334323443
            300
            1
            2009-10-07
            Family
            4
        
        '''%resort

        if full:
            url = '/api/rest/Review?type=full'
        else:
            url = '/api/rest/Review'     

        return POST(self.url(url), body=xml, async=False, resp=True)

Updating an item


   def test_updating_review(self):
        'Vacation :: Updating review'

        response, resort = self.create_resort()
        response, review_id = self.create_review(resort)

        xml = '''
        
            5
            5
            Wedding
            5
            %s
            334323443
            520
            true
            2009-10-07
            Family
            4
        
        '''%resort
        response, content = PUT(self.url('/api/rest/Review/%s?type=full'%review_id), body=xml, resp=True, async=False )

        expected = '''
        
            %s
            5
            5
            Wedding
            5
            %s
            334323443
            520
            true
            2009-10-07
            Family
            4
        
        '''%(review_id,resort)

        expected = objectify.fromstring(expected)
        expected = etree.tostring(expected)

        result = objectify.fromstring(content)
        result = etree.tostring(result)

        assert_equals(expected, result)

I hope you find these helpful

Information and Links

Join the fray by commenting, tracking what others have to say, or linking to it from your blog.


Other Posts
3scale.net integration is complete
API Testing Code is ready

Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.




Reader Comments

Be the first to leave a comment!