Example #1
0
 def test_post_to_create_a_new_resource(self):
     a_new_item = {'text': 'this is my new item'}
     response = self.post(self.get_url('/api'), dumps(a_new_item))
     assert_response_code(response, 201)
     self.assertRegexpMatches(response.headers['Location'],
                              r'http://localhost:\d+/api/\d+')
     assert loads(response.body)['text'] == 'this is my new item'
Example #2
0
 def test_post_to_create_a_new_resource(self):
     a_new_item = {
         'text': 'this is my new item'
     }
     response = self.post(self.get_url('/api'), dumps(a_new_item))
     assert_response_code(response, 201)
     assert 'Location' in response.headers
Example #3
0
 def test_jsonp_response_when_accept_textjavascript(self):
     response = self._fetch(
         self.get_url('/api/?callback=my_callback'), 'GET', headers={
             'Accept': 'text/javascript'
         })
     assert_response_code(response, 200)
     assert response.body.decode('utf-8').startswith('my_callback(')
Example #4
0
 def test_return_resource_as_xml(self):
     url = self.get_url('/api/1')
     response = self._fetch(url, 'GET', headers=dict(Accept='text/xml'))
     assert_response_code(response, 200)
     assert 'text/xml' in response.headers[
         'Content-Type'], 'the content-type should be text/xml but it was {0}'.format(
             response.headers['Content-Type'])
     assert response.body == b'<comment id="1">X</comment>'
Example #5
0
 def test_choose_response_type_based_on_the_accept_header(self):
     url = self.get_url('/api/1')
     response = self._fetch(
         url, 'GET', headers={'Accept': 'application/json, text/xml'})
     assert_response_code(response, 200)
     assert 'application/json' in response.headers[
         'Content-Type'], 'the content-type should be application/json but it was {0}'.format(
             response.headers['Content-Type'])
Example #6
0
 def test_post_to_create_a_new_resource(self):
     a_new_item = {
         'text': 'this is my new item'
     }
     response = self.post(self.get_url('/api'), dumps(a_new_item))
     assert_response_code(response, 201)
     self.assertRegexpMatches(response.headers['Location'], r'http://localhost:\d+/api/\d+')
     assert loads(response.body)['text'] == 'this is my new item'
Example #7
0
 def test_get_a_specific_resource_using_get_request(self):
     response = self.get('/api/3')
     assert_response_code(response, 200)
     resource = loads(response.body.decode('utf-8'))
     assert 'id' in resource, 'should have the key \'id\' in the resource instance {0!s}'.format(
         resource)
     assert 'text' in resource, 'should have the \'text\' in the resource instance {0!s}'.format(
         resource)
Example #8
0
 def test_show_content_as_html_when_requested_by_browser(self):
     CHROME_ACCEPT_HEADER = 'text/html,application/xhtml+xml,application/xm'\
                            'l;q=0.9,*/*;q=0.8'
     response = self._fetch(self.get_url('/api/'),
                            'GET',
                            headers={'Accept': CHROME_ACCEPT_HEADER})
     assert_response_code(response, 200)
     assert '<body>' in response.body.decode('utf-8')
Example #9
0
 def test_show_content_as_html_when_requested_by_browser(self):
     CHROME_ACCEPT_HEADER = 'text/html,application/xhtml+xml,application/xm'\
                            'l;q=0.9,*/*;q=0.8'
     response = self._fetch(
         self.get_url('/api/'), 'GET', headers={
             'Accept': CHROME_ACCEPT_HEADER
         })
     assert_response_code(response, 200)
     assert '<body>' in response.body.decode('utf-8')
Example #10
0
 def test_get_request_to_list_all_resource_instances(self):
     response = self.get('/api')
     assert_response_code(response, 200)
     resources = loads(response.body.decode('utf-8'))
     number_of_items = len(resources)
     assert number_of_items == 10, 'should return 10 resources but returned {0:d}'.format(number_of_items)
     for item in resources:
         assert 'id' in item, 'should have the key \'id\' in the resource instance'
         assert 'text' in item, 'should have the \'text\' in the resource instance'
Example #11
0
 def test_get_request_to_list_all_resource_instances(self):
     response = self.get('/api')
     assert_response_code(response, 200)
     resources = loads(response.body.decode('utf-8'))
     number_of_items = len(resources)
     assert number_of_items == 10, 'should return 10 resources but returned {0:d}'.format(
         number_of_items)
     for item in resources:
         assert 'id' in item, 'should have the key \'id\' in the resource instance'
         assert 'text' in item, 'should have the \'text\' in the resource instance'
Example #12
0
 def test_update_new_instance_of_the_resource_with_content_type_text_xml(self):
     an_updated_item ='<comment id="2">meu comentario</comment>'
     response = self._fetch(self.get_url('/api/2'), 'PUT', headers={'Content-Type': 'text/xml'}, body=an_updated_item)
     assert_response_code(response, 204)
     # get the resource to verify if it was updated
     response = self._fetch(response.headers['Location'], 'GET', headers={'Accept': 'text/xml'})
     assert 'text/xml' in response.headers['Content-Type'], 'the content-type should be text/xml but it was {0}'.format(response.headers['Content-Type'])
     doc = ElementTree.fromstring(response.body)
     assert doc.tag == 'comment', 'the tag should be "comment" but it was {0}'.format(doc.tag)
     assert doc.text == 'meu comentario', 'the comment text should be "meu comentario" but it was {0}'.format(doc.text)
Example #13
0
 def test_put_to_update_an_existing_resource(self):
     response = self.get('/api/1')
     assert_response_code(response, 200)
     resource = loads(response.body.decode('utf-8'))
     resource['comment'] = 'wow!'
     response = self.put(self.get_url('/api/1'), dumps(resource))
     assert_response_code(response, 204)
     response = self.get('/api/1')
     resource = loads(response.body.decode('utf-8'))
     assert 'comment' in resource
     assert resource['comment'] == 'wow!'
Example #14
0
 def test_create_new_instance_of_the_resource_with_content_type_text_xml(self):
     a_new_item ='<comment>meu comentario</comment>'
     response = self._fetch(self.get_url('/api'), 'POST', headers={'Content-Type': 'text/xml'}, body=a_new_item)
     assert_response_code(response, 201)
     # gets the new instance
     response = self._fetch(response.headers['Location'], 'GET', headers={'Accept': 'text/xml'})
     assert 'text/xml' in response.headers['Content-Type'], 'the content-type should be text/xml but it was {0}'.format(response.headers['Content-Type'])
     doc = ElementTree.fromstring(response.body)
     assert doc.tag == 'comment', 'the tag should be "comment" but it was {0}'.format(doc.tag)
     assert doc.text == 'meu comentario', 'the comment text should be "meu comentario" but it was {0}'.format(doc.text)
     assert doc.get('id') == '10', 'the id should be 11 but it was {0}'.format(doc.get('id'))
Example #15
0
 def test_put_to_update_an_existing_resource(self):
     response = self.get('/api/1')
     assert_response_code(response, 200)
     resource = loads(response.body.decode('utf-8'))
     resource['comment'] = 'wow!'
     response = self.put(self.get_url('/api/1'), dumps(resource))
     assert_response_code(response, 204)
     response = self.get('/api/1')
     resource = loads(response.body.decode('utf-8'))
     assert 'comment' in resource
     assert resource['comment'] == 'wow!'
Example #16
0
 def test_update_new_instance_of_the_resource_with_content_type_text_xml(
         self):
     an_updated_item = '<comment id="2">meu comentario</comment>'
     response = self._fetch(self.get_url('/api/2'),
                            'PUT',
                            headers={'Content-Type': 'text/xml'},
                            body=an_updated_item)
     assert_response_code(response, 204)
     # get the resource to verify if it was updated
     response = self._fetch(response.headers['Location'],
                            'GET',
                            headers={'Accept': 'text/xml'})
     assert 'text/xml' in response.headers[
         'Content-Type'], 'the content-type should be text/xml but it was {0}'.format(
             response.headers['Content-Type'])
     doc = ElementTree.fromstring(response.body)
     assert doc.tag == 'comment', 'the tag should be "comment" but it was {0}'.format(
         doc.tag)
     assert doc.text == 'meu comentario', 'the comment text should be "meu comentario" but it was {0}'.format(
         doc.text)
Example #17
0
 def test_create_new_instance_of_the_resource_with_content_type_text_xml(
         self):
     a_new_item = '<comment>meu comentario</comment>'
     response = self._fetch(self.get_url('/api'),
                            'POST',
                            headers={'Content-Type': 'text/xml'},
                            body=a_new_item)
     assert_response_code(response, 201)
     # gets the new instance
     response = self._fetch(response.headers['Location'],
                            'GET',
                            headers={'Accept': 'text/xml'})
     assert 'text/xml' in response.headers[
         'Content-Type'], 'the content-type should be text/xml but it was {0}'.format(
             response.headers['Content-Type'])
     doc = ElementTree.fromstring(response.body)
     assert doc.tag == 'comment', 'the tag should be "comment" but it was {0}'.format(
         doc.tag)
     assert doc.text == 'meu comentario', 'the comment text should be "meu comentario" but it was {0}'.format(
         doc.text)
     assert doc.get(
         'id') == '10', 'the id should be 11 but it was {0}'.format(
             doc.get('id'))
Example #18
0
 def test_return_resource_as_xml(self):
     url = self.get_url('/api/1')
     response = self._fetch(url, 'GET', headers=dict(Accept='text/xml'))
     assert_response_code(response, 200)
     assert 'text/xml' in response.headers['Content-Type'], 'the content-type should be text/xml but it was {0}'.format(response.headers['Content-Type'])
     assert response.body == b'<comment id="1">X</comment>'
Example #19
0
 def test_try_to_update_a_resource_that_does_not_exist(self):
     response = self.put(self.get_url('/api/30'), dumps(dict(text='not exist')))
     assert_response_code(response, 404)
Example #20
0
 def test_should_throw_error_when_required_param_is_not_passed(self):
     response = self.get('/projects.json')
     assert_response_code(response, 400)
     assert loads(response.body)['error'] == \
         'The "ck" parameter is required.'
Example #21
0
 def test_get_a_specific_resource_using_get_request(self):
     response = self.get('/api/3')
     assert_response_code(response, 200)
     resource = loads(response.body.decode('utf-8'))
     assert 'id' in resource, 'should have the key \'id\' in the resource instance {0!s}'.format(resource)
     assert 'text' in resource, 'should have the \'text\' in the resource instance {0!s}'.format(resource)
Example #22
0
 def test_try_to_delete_a_resource(self):
     response = self.delete(self.get_url('/api/1'))
     assert_response_code(response, 404)
Example #23
0
 def test_try_to_get_instance(self):
     response = self.get('/api/1')
     assert_response_code(response, 404)
Example #24
0
 def test_should_be_able_to_call_without_size(self):
     response = self.get('/projects.json?ck=1&name=foobar')
     assert_response_code(response, 200)
Example #25
0
 def test_should_return_an_descriptive_error(self):
     response = self.get('/projects.json?ck=1&name=test&size=foo')
     assert_response_code(response, 400)
     assert loads(response.body)['error'] == \
         'The "size" parameter value is not valid.'
Example #26
0
 def test_should_be_able_to_call_without_size(self):
     response = self.get('/projects.json?ck=1&name=foobar')
     assert_response_code(response, 200)
Example #27
0
 def test_delete_method_to_destroy_a_resource(self):
     response = self.delete(self.get_url('/api/1'))
     assert_response_code(response, 200)
     response = self.delete(self.get_url('/api/1'))
     assert_response_code(response, 404)
Example #28
0
 def test_try_to_update_a_resource_that_does_not_exist(self):
     response = self.put(self.get_url('/api/30'),
                         dumps(dict(text='not exist')))
     assert_response_code(response, 404)
Example #29
0
 def test_should_throw_error_when_required_param_is_not_passed(self):
     response = self.get('/projects.json')
     assert_response_code(response, 400)
     assert loads(response.body)['error'] == \
         'The "ck" parameter is required.'
Example #30
0
 def test_jsonp_response_when_accept_textjavascript(self):
     response = self._fetch(self.get_url('/api/?callback=my_callback'),
                            'GET',
                            headers={'Accept': 'text/javascript'})
     assert_response_code(response, 200)
     assert response.body.decode('utf-8').startswith('my_callback(')
Example #31
0
 def test_try_to_list_resources(self):
     response = self.get('/api')
     assert_response_code(response, 404)
Example #32
0
 def test_use_the_default_encoder(self):
     response = self._fetch(self.get_url('/api/?callback=my_callback'),
                            'GET',
                            headers={'Accept': 'lol/cat'})
     assert_response_code(response, 200)
Example #33
0
 def test_try_to_update_a_resource(self):
     response = self.put(self.get_url('/api/1'), dumps(dict(text='nice')))
     assert_response_code(response, 404)
Example #34
0
 def test_should_return_ok_if_more_params_than_expected(self):
     response = self.get('/projects.json?ck=1&name=test&foo=bar&size=234')
     assert_response_code(response, 200)
Example #35
0
 def test_should_be_a_valid_using_int(self):
     response = self.get('/always_valid.json?size=1')
     assert_response_code(response, 200)
Example #36
0
 def test_should_be_a_valid_request_anyway(self):
     response = self.get('/always_valid.json?size=abc')
     assert_response_code(response, 200)
Example #37
0
 def test_get_a_resource_that_does_not_exist(self):
     response = self.get('/api/30')
     assert_response_code(response, 404)
Example #38
0
 def test_should_return_type_xml_as_specified_in_url(self):
     response = self.get('/api/1.xml')
     assert_response_code(response, 200)
     assert '</comment>' in response.body.decode('utf-8')
Example #39
0
 def test_should_return_type_json_as_specified_in_url(self):
     response = self.get('/api/1.js?callback=myCallbackFooBar')
     assert_response_code(response, 200)
     assert response.body.decode('utf-8').startswith('myCallbackFooBar(')
Example #40
0
 def test_should_return_with_the_callback_name_i_choose(self):
     response = self.get('/api/1.js?callback=fooBar')
     assert_response_code(response, 200)
     assert response.body.decode('utf-8').startswith('fooBar(')
Example #41
0
 def test_delete_method_to_destroy_a_resource(self):
     response = self.delete(self.get_url('/api/1'))
     assert_response_code(response, 200)
     response = self.delete(self.get_url('/api/1'))
     assert_response_code(response, 404)
Example #42
0
 def test_try_to_list_resources(self):
     response = self.get('/api')
     assert_response_code(response, 404)
Example #43
0
 def test_choose_response_type_based_on_the_accept_header(self):
     url = self.get_url('/api/1')
     response = self._fetch(url, 'GET', headers={'Accept':'application/json, text/xml'})
     assert_response_code(response, 200)
     assert 'application/json' in response.headers['Content-Type'], 'the content-type should be application/json but it was {0}'.format(response.headers['Content-Type'])
Example #44
0
 def test_try_to_update_a_resource(self):
     response = self.put(self.get_url('/api/1'), dumps(dict(text='nice')))
     assert_response_code(response, 404)
Example #45
0
 def test_should_return_type_json_as_specified_in_url(self):
     response = self.get('/api/1.json')
     assert_response_code(response, 200)
     data = loads(response.body.decode('utf-8'))
     assert 'id' in data.decode('utf-8')
Example #46
0
 def test_get_a_resource_that_does_not_exist(self):
     response = self.get('/api/30')
     assert_response_code(response, 404)
Example #47
0
 def test_use_the_default_encoder(self):
     response = self._fetch(
         self.get_url('/api/?callback=my_callback'), 'GET', headers={
             'Accept': 'lol/cat'
         })
     assert_response_code(response, 200)
Example #48
0
 def test_should_return_type_xml_as_specified_in_url(self):
     response = self.get('/api/1.xml')
     assert_response_code(response, 200)
     assert '</comment>' in response.body.decode('utf-8')
Example #49
0
 def test_should_return_type_json_as_specified_in_url(self):
     response = self.get('/api/1.json')
     assert_response_code(response, 200)
     data = loads(response.body.decode('utf-8'))
     assert 'id' in data.decode('utf-8')
Example #50
0
 def test_should_raise_404_when_extension_is_not_found(self):
     response = self.get('/api/1.rb')
     assert_response_code(response, 404)
Example #51
0
 def test_should_raise_404_when_extension_is_not_found(self):
     response = self.get('/api/1.rb')
     assert_response_code(response, 404)
Example #52
0
 def test_should_return_type_json_as_specified_in_url(self):
     response = self.get('/api/1.js?callback=myCallbackFooBar')
     assert_response_code(response, 200)
     assert response.body.decode('utf-8').startswith('myCallbackFooBar(')
Example #53
0
 def test_should_return_the_default_callback_when_i_not_specify_in_my_request(self):
     response = self.get('/api/1.js')
     assert_response_code(response, 200)
     assert re.match(b'^thePersonalizedCallback\(.*', response.body), response.body.decode('utf-8')
Example #54
0
 def test_should_return_the_default_callback_when_i_not_specify_in_my_request(
         self):
     response = self.get('/api/1.js')
     assert_response_code(response, 200)
     assert re.match(b'^thePersonalizedCallback\(.*',
                     response.body), response.body.decode('utf-8')
Example #55
0
 def test_should_return_cross_origin_header(self):
     response = self.get('/api/1.js?callback=fooBar')
     assert_response_code(response, 200)
     assert 'Access-Control-Allow-Origin' in response.headers
     assert response.headers['Access-Control-Allow-Origin'] == '*'
Example #56
0
 def test_should_return_with_the_callback_name_i_choose(self):
     response = self.get('/api/1.js?callback=fooBar')
     assert_response_code(response, 200)
     assert response.body.decode('utf-8').startswith('fooBar(')
Example #57
0
 def test_try_to_get_instance(self):
     response = self.get('/api/1')
     assert_response_code(response, 404)
Example #58
0
 def test_should_return_cross_origin_header(self):
     response = self.get('/api/1.js?callback=fooBar')
     assert_response_code(response, 200)
     assert 'Access-Control-Allow-Origin' in response.headers
     assert response.headers['Access-Control-Allow-Origin'] == '*'
Example #59
0
 def test_try_to_delete_a_resource(self):
     response = self.delete(self.get_url('/api/1'))
     assert_response_code(response, 404)
Example #60
0
 def test_should_return_an_descriptive_error(self):
     response = self.get('/projects.json?ck=1&name=test&size=foo')
     assert_response_code(response, 400)
     assert loads(response.body)['error'] == \
         'The "size" parameter value is not valid.'