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'
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
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(')
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>'
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'])
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'
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)
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')
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')
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'
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'
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)
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!'
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'))
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)
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'))
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>'
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)
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.'
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)
def test_try_to_delete_a_resource(self): response = self.delete(self.get_url('/api/1')) assert_response_code(response, 404)
def test_try_to_get_instance(self): response = self.get('/api/1') assert_response_code(response, 404)
def test_should_be_able_to_call_without_size(self): response = self.get('/projects.json?ck=1&name=foobar') assert_response_code(response, 200)
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.'
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)
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(')
def test_try_to_list_resources(self): response = self.get('/api') assert_response_code(response, 404)
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)
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)
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)
def test_should_be_a_valid_using_int(self): response = self.get('/always_valid.json?size=1') assert_response_code(response, 200)
def test_should_be_a_valid_request_anyway(self): response = self.get('/always_valid.json?size=abc') assert_response_code(response, 200)
def test_get_a_resource_that_does_not_exist(self): response = self.get('/api/30') assert_response_code(response, 404)
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')
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(')
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(')
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'])
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')
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)
def test_should_raise_404_when_extension_is_not_found(self): response = self.get('/api/1.rb') assert_response_code(response, 404)
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')
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')
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'] == '*'