Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with posting with no content #120

Merged
merged 4 commits into from
May 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions flask_potion/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def convert(self, instance, update=False, pre_resolved_properties=None, patchabl

def parse_request(self, request):
if request.method in ('POST', 'PATCH', 'PUT', 'DELETE'):
if request.mimetype != 'application/json':
if self.fields and request.mimetype != 'application/json':
raise RequestMustBeJSON()

# TODO change to request.get_json(silent=False) to catch invalid JSON
Expand All @@ -281,4 +281,3 @@ def parse_request(self, request):
pass

return self.convert(data, update=request.method in ('PUT', 'PATCH'), patchable=request.method == 'PATCH')

33 changes: 28 additions & 5 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
from unittest import TestCase
from flask import Flask
from flask import Flask, request, Request
from flask_potion import fields, Resource
from flask_potion.exceptions import ValidationError
from flask_potion.schema import Schema, FieldSet

from flask_potion.schema import Schema, FieldSet, RequestMustBeJSON
from sys import version_info
if version_info.major < 3:
from StringIO import StringIO
else:
from io import StringIO

class SchemaTestCase(TestCase):

def test_schema_class(self):
class FooSchema(Schema):

def __init__(self, schema):
self._schema = schema

Expand Down Expand Up @@ -99,7 +103,26 @@ class BarResource(Resource):
FieldSet({"foo": fields.String()}).bind(FooResource).bind(BarResource)

def test_fieldset_parse_request(self):
pass
app = Flask(__name__)
env = {}
with app.test_request_context():
env = request.environ

# Ensure allow empty POST
fs = FieldSet(None)
env['REQUEST_METHOD'] = 'POST'
fs.parse_request(Request(env))

# Ensure failure when there are fields
fs = FieldSet({'field': fields.String()})
with self.assertRaises(RequestMustBeJSON):
fs.parse_request(Request(env))

# Successful POST with data
env['wsgi.input'] = StringIO('{"field": "data"}')
env['CONTENT_TYPE'] = 'application/json'
env['CONTENT_LENGTH'] = '17'
fs.parse_request(Request(env))

def test_fieldset_format(self):
self.assertEqual(
Expand Down