ElSnoMan / pyclinic

A library to test services in Python
MIT License
5 stars 1 forks source link

Request needs to send json field, but data is the default #37

Closed ElSnoMan closed 3 years ago

ElSnoMan commented 3 years ago

Why would my test_create_todo() not be working here? The test_get_todos() works. When I just build it out manually with requests it works. I can post this on your git repo instead if you want. BTW did slack get rid of the big code snippet with line numbers? I can't seem to find it.

import pytest
from pyclinic.postman import Postman

@pytest.fixture
def todo_runner():
    return Postman("Todo-Flask-Heroku.postman_collection.json")
import requests
from pyclinic.postman import Postman
from requests.models import Response

def test_get_todos(todo_runner):
    response = todo_runner.Root.get_todos()
    assert response.status_code == 200

def test_create_todo(todo_runner):
    response = todo_runner.Root.post_todo()
    assert response.status_code == 200

def test_create_todo_with_requests():
    data = {"title": "hello from request", "done": False}
    response = requests.post(
        "https://df-flask-todo-api-class.herokuapp.com/todo", json=data
    )
    assert response.status_code == 200

I also tried it like this:

def test_create_todo(todo_runner):
    # response = todo_runner.Root.post_todo()
    response = todo_runner.Root.post_todo(
        data={"title": "hello from request", "done": False}
    )
    assert response.status_code == 200

Todo-Flask-Heroku.postman_collection.json

ElSnoMan commented 3 years ago

@gleekzorp

When I inspected the request with runner.Root.post_todo.help(), I saw that the headers had the Content-Type set to application/json. Because of this, you need to replace the default data field with a json field:

runner = Postman(path)
runner.Root.post_todo.help()  # 1

req = runner.Root.post_todo.request
req["json"] = req.pop("data")

runner.Root.post_todo.help()  # 2
response = runner.Root.post_todo()
assert response.ok

The request dictionary to send at #1

{
    'method': 'POST',
    'url': 'https://df-flask-todo-api-class.herokuapp.com/todo',
    'data': {'title': 'check new stuff', 'done': False},
    'headers': {'Content-Type': 'application/json'}
}

After renaming data to json, here's what the request looks like in #2

{
    'method': 'POST',
    'url': 'https://df-flask-todo-api-class.herokuapp.com/todo',
    'headers': {'Content-Type': 'application/json'},
    'json': {'title': 'check new stuff', 'done': False}
}