def test_haltalk_root(self): h = HALEasy('http://haltalk.herokuapp.com.test_domain') self.assertEqual(h.link(rel=u'self')['href'], u'/') self.assertEqual( h.link(rel=u'http://haltalk.herokuapp.com/rels/users')['href'], u'/users') self.assertEqual( h.link(rel=u'http://haltalk.herokuapp.com/rels/me')['href'], u'/users/{name}') self.assertEqual( h.link(rel=u'http://haltalk.herokuapp.com/rels/me')['templated'], True)
def test_can_extract_specific_link(self): h = HALEasy('http://api.test_domain/api_root') self.assertDictEqual( h.link(rel='other:link5').as_object(), { "name": "link5name1", "href": "/link5path1", })
def test_haltalk_create_user(self): h = HALEasy('http://haltalk.herokuapp.com.test_domain') user = h.link(rel='ht:signup').follow(method='POST', data={ 'username': '******', 'password': '******' }) self.assertEqual(user['username'], 'aaa')
def test_full_object_fetched_when_preview_lacks_property(self): h = HALEasy('http://api.test_domain/api_root') h1 = h.link(rel="sample_hal_rel1").follow() self.assertEqual( h1['k'], 'l') # 'k' not in embedded resource properties, HTTP GET performed self.assertFalse(h1.is_preview) # h1 is now not an embedded resource self.assertEqual(h1['i'], 'x') # value of h1['i'] has changed to 'x' self.assertEqual(h1.preview['i'], 'j') # old value of h1['i'] available here
def test_link_variable_expansion(self): thing4json = json.dumps({ "_links": { "self": { "href": "/link4path/foo" } }, "prop1": "val1" }) responses.add(responses.GET, 'http://api.test_domain/link4path/foo', body=thing4json, status=200, content_type='application/json') h = HALEasy('http://api.test_domain/api_root') h2 = h.link(rel="link4").follow(var="foo") self.assertEqual(h2.fetched_from, 'http://api.test_domain/link4path/foo')
def test_haltalk_get_me_aaa(self): h = HALEasy('http://haltalk.herokuapp.com.test_domain') user = h.link(rel='ht:me').follow(name='aaa') self.assertEqual(user['username'], 'aaa')
def test_find_named_rel_by_name(self): h = HALEasy('http://api.test_domain/api_root') l = h.link(name="thing3")
import string, random from haleasy import HALEasy name = ''.join( random.choice(string.ascii_uppercase + string.digits) for _ in range(12)) h = HALEasy('http://haltalk.herokuapp.com/') l = h.link(rel='http://haltalk.herokuapp.com/rels/signup') s = l.follow(method='POST', data={'username': name, 'password': '******'}) print(s.doc.as_object()) print(s.properties())
def test_links_have_preview_attributes(self): h = HALEasy('http://api.test_domain/api_root') self.assertTrue(h.link(rel='sample_hal_rel1').preview) self.assertTrue(h.link(rel='sample_hal_rel4').preview)
def test_blank_href(self): h = HALEasy('http://api.test_domain/api_root') l = h.link(rel="blank_href") h2 = l.follow() self.assertEqual(h2['p'], 'q')
def test_nolinks(self): h = HALEasy('http://api.test_domain/api_root') l = h.link(rel="nolinks") h2 = l.follow() self.assertEqual(h2['p'], 'q')
def test_find_embedded_rel_by_property_in_self_link_when_no_link_from_resource( self): h = HALEasy('http://api.test_domain/api_root') h2 = h.link(rel="sample_hal_rel2", name="thing2").follow() self.assertEqual(h2['e'], 'f')
def test_embedded_rel_with_multiple_objects(self): h = HALEasy('http://api.test_domain/api_root') links = list(h.links(rel="sample_hal_rel2")) self.assertEqual(len(links), 2) h2 = h.link(rel="sample_hal_rel2").follow() self.assertEqual(h2['e'], 'f')
def test_embedded_object_has_correct_attributes(self): h = HALEasy('http://api.test_domain/api_root') h1 = h.link(rel="sample_hal_rel1").follow() self.assertTrue(h1['a'] == 'b') # h1 is an embedded resource
def test_embedded_object_has_preview_true(self): h = HALEasy('http://api.test_domain/api_root') h1 = h.link(rel="sample_hal_rel1").follow() self.assertTrue(h1.is_preview) # h1 is an embedded resource