-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
62 lines (48 loc) · 1.93 KB
/
test_api.py
File metadata and controls
62 lines (48 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# Author: Arne Neumann <nlpbox.programming@arne.cl>
"""Tests for the mock RST parser."""
from __future__ import print_function
import os
import pexpect
import pytest
import requests
from app import RS3_OUTPUT_TEMPLATE
@pytest.fixture(scope="session", autouse=True)
def start_api():
"""Starts the REST API in the background."""
print("starting API...")
child = pexpect.spawn('python app.py')
yield child.expect('(?i)Running on http://0.0.0.0:5000/') # provide the fixture value
print("stopping API...")
child.close()
def post_input_file(input_text):
"""Posts a string as a file (multipart/form-data) named 'input' to the
REST API and returns the response.
"""
url = 'http://localhost:5000/parse'
return requests.post(url, files={'input': input_text})
def post_input_form(input_text):
"""Posts a string as a form field (application/x-www-form-urlencoded)
named 'input' to the REST API and returns the response.
"""
url = 'http://localhost:5000/parse'
return requests.post(url, data={'input': input_text})
def test_rst_parser_file():
"""API generates rs3 file from plain text input (multipart/form-data)"""
input_string = 'foo'
res = post_input_file(input_string)
expected_output = RS3_OUTPUT_TEMPLATE.format(begin=input_string, end=input_string)
assert res.content.decode('utf-8') == expected_output
def test_rst_parser_form():
"""API generates rs3 file from plain text input (application/x-www-form-urlencoded)"""
input_string = 'foo'
res = post_input_file(input_string)
expected_output = RS3_OUTPUT_TEMPLATE.format(begin=input_string, end=input_string)
assert res.content.decode('utf-8') == expected_output
def test_missing_input():
"""Calling the API with missing results in an error"""
# no input file
url = 'http://localhost:5000/parse'
res = requests.post(url)
assert res.status_code != 200