Esempio n. 1
0
def test_go_the_distance():
    # GET root
    response, content = http.request('http://0.0.0.0:8080/',
            headers={'Accept': 'application/hal+json'})
    assert response['status'] == '200'
    assert response['content-type'] == 'application/hal+json; charset=UTF-8'

    document = HalDocument.from_json(content)
    resolver = Resolver(document.get_curies())
    links = document.links

    assert 'tiddlyweb:bags' in links
    assert (resolver.expand('tiddlyweb:bags')
            == 'http://tiddlyweb.com/relations/bags')

    target_link = links['tiddlyweb:bags']['href']
    assert target_link == 'http://0.0.0.0:8080/bags'

    # GET bags
    response, content = http.request(target_link,
            headers={'Accept': 'application/hal+json'})
    assert response['status'] == '200'
    assert response['content-type'] == 'application/hal+json; charset=UTF-8'

    document = HalDocument.from_json(content)
    links = document.links

    data = document.get_data('tiddlyweb:bag')
    assert len(data) == 0

    bags_link = target_link

    # Make a bag
    assert 'tiddlyweb:bag' in links
    target_link = expand(links['tiddlyweb:bag']['href'], {'bag': 'mybag'})
    assert target_link == 'http://0.0.0.0:8080/bags/mybag'

    response, content = http.request(target_link,
            method='PUT',
            headers={'Content-Type': 'application/json'},
            body='{"desc": "my own special bag"}')

    assert response['status'] == '204'
    assert response['location'] == target_link

    # GET bags again, this time there is one
    response, content = http.request(bags_link,
            headers={'Accept': 'application/hal+json'})
    assert response['status'] == '200'
    assert response['content-type'] == 'application/hal+json; charset=UTF-8'

    document = HalDocument.from_json(content)
    links = document.links

    data = document.get_data('tiddlyweb:bag')
    assert len(data) == 1
    assert data[0]['name'] == 'mybag'

    # GET the single bag
    response, content = http.request(target_link,
            headers={'Accept': 'application/hal+json'})
    assert response['status'] == '200'
    assert response['content-type'] == 'application/hal+json; charset=UTF-8'
    document = HalDocument.from_json(content)
    links = document.links

    assert 'tiddlyweb:tiddlers' in links

    target_link = links['tiddlyweb:tiddlers']['href']

    # GET the (empty) tiddlers
    response, content = http.request(target_link,
            headers={'Accept': 'application/hal+json'})
    assert response['status'] == '200', content
    assert response['content-type'] == 'application/hal+json; charset=UTF-8'
    document = HalDocument.from_json(content)
    links = document.links

    data = document.get_data('tiddlyweb:tiddler')
    assert len(data) == 0

    tiddlers_link = target_link

    target_link = expand(links['tiddlyweb:tiddler']['href'],
            {'tiddler': 'mytiddler'})
    response, content = http.request(target_link,
            method='PUT',
            headers={'Content-Type': 'application/json'},
            body='{"text": "oh hai", "tags": ["tagone", "tagtwo"]}')

    assert response['status'] == '204'
    assert response['location'] == target_link

    # GET the (no longer empty) tiddlers
    response, content = http.request(tiddlers_link,
            headers={'Accept': 'application/hal+json'})
    assert response['status'] == '200', content
    assert response['content-type'] == 'application/hal+json; charset=UTF-8'
    document = HalDocument.from_json(content)
    links = document.links

    data = document.get_data('tiddlyweb:tiddler')
    assert len(data) == 1

    assert data[0]['title'] == 'mytiddler'