<s> import json <EOL> from urllib . request import urlopen , Request <EOL> from urllib . parse import urlencode <EOL> import flask_httpretty <EOL> import pytest <EOL> HOST = '<STR_LIT>' <EOL> @ pytest . fixture <EOL> def f_monkeypatch ( f_app ) : <EOL> flask_httpretty . register_app ( f_app , HOST ) <EOL> flask_httpretty . enable ( ) <EOL> def uri ( path ) : <EOL> if not path . endswith ( '<STR_LIT:/>' ) : <EOL> path += '<STR_LIT:/>' <EOL> return '<STR_LIT>' . format ( HOST , path ) <EOL> def test_pretty_get ( f_monkeypatch ) : <EOL> body = '<STR_LIT>' <EOL> resp = urlopen ( uri ( '<STR_LIT>' ) ) . read ( ) <EOL> assert body == resp . decode ( '<STR_LIT:utf-8>' ) <EOL> def test_pretty_post ( f_monkeypatch ) : <EOL> body = { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> payload = urlencode ( body ) . encode ( '<STR_LIT:utf-8>' ) <EOL> req = Request ( uri ( '<STR_LIT>' ) , payload , method = '<STR_LIT:POST>' ) <EOL> resp = urlopen ( req ) . read ( ) <EOL> assert body == json . loads ( resp . decode ( '<STR_LIT:utf-8>' ) ) <EOL> def test_pretty_post_by_json ( f_monkeypatch ) : <EOL> body = { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> payload = json . dumps ( body ) . encode ( '<STR_LIT:utf-8>' ) <EOL> req = Request ( uri ( '<STR_LIT>' ) , payload , method = '<STR_LIT:POST>' , <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } ) <EOL> resp = urlopen ( req ) . read ( ) <EOL> assert body == json . loads ( resp . decode ( '<STR_LIT:utf-8>' ) ) <EOL> def test_pretty_put ( f_monkeypatch ) : <EOL> body = { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> payload = urlencode ( body ) . encode ( '<STR_LIT:utf-8>' ) <EOL> req = Request ( uri ( '<STR_LIT>' ) , payload , method = '<STR_LIT>' ) <EOL> resp = urlopen ( req ) . read ( ) <EOL> assert '<STR_LIT:{}>' == resp . decode ( '<STR_LIT:utf-8>' ) <EOL> def test_pretty_delete ( f_monkeypatch ) : <EOL> req = Request ( uri ( '<STR_LIT>' ) , method = '<STR_LIT>' ) <EOL> resp = urlopen ( req ) . read ( ) <EOL> assert '<STR_LIT:{}>' == resp . decode ( '<STR_LIT:utf-8>' ) <EOL> def test_pretty_get_with_query ( f_monkeypatch ) : <EOL> query = '<STR_LIT>' <EOL> resp = urlopen ( uri ( '<STR_LIT>' ) + '<STR_LIT>' . format ( query ) ) . read ( ) <EOL> assert query == resp . decode ( '<STR_LIT:utf-8>' ) </s>
