Beispiel #1
0
def test_raise_error_when_no_default_and_no_status_code_match(
        empty_swagger_spec, op_spec):
    del op_spec['responses']['default']
    op = Operation(empty_swagger_spec, '/pet/{petId}', 'get', op_spec)
    with pytest.raises(MatchingResponseNotFound) as excinfo:
        get_response_spec(404, op)
    assert 'not found' in str(excinfo.value)
Beispiel #2
0
def test_complicated_refs():
    # Split the swagger spec into a bunch of different json files and use
    # $refs all over to place to wire stuff together - see the test-data
    # files or this will make no sense whatsoever.
    file_path = '../../test-data/2.0/simple_crossref/swagger.json'
    swagger_dict, origin_url = get_spec_json_and_url(file_path)
    swagger_spec = Spec.from_dict(swagger_dict, origin_url=origin_url)

    # Verify things are 'reachable' (hence, have been ingested correctly)

    # Resource
    assert swagger_spec.resources['pingpong']

    # Operation
    op = swagger_spec.resources['pingpong'].ping
    assert op

    # Parameter
    assert swagger_spec.resources['pingpong'].ping.params['pung']

    # Parameter name
    assert swagger_spec.resources['pingpong'].ping.params[
        'pung'].name == 'pung'

    # Response
    response = get_response_spec(200, op)
    assert response['description'] == 'pong'
Beispiel #3
0
def test_complicated_refs():
    # Split the swagger spec into a bunch of different json files and use
    # $refs all over to place to wire stuff together - see the test-data
    # files or this will make no sense whatsoever.
    file_path = "../../test-data/2.0/simple_crossref/swagger.json"
    swagger_dict, origin_url = get_spec_json_and_url(file_path)
    swagger_spec = Spec.from_dict(swagger_dict, origin_url=origin_url)

    # Verify things are 'reachable' (hence, have been ingested correctly)

    # Resource
    assert swagger_spec.resources["pingpong"]

    # Operation
    op = swagger_spec.resources["pingpong"].ping
    assert op

    # Parameter
    assert swagger_spec.resources["pingpong"].ping.params["pung"]

    # Parameter name
    assert swagger_spec.resources["pingpong"].ping.params["pung"].name == "pung"

    # Response
    response = get_response_spec(200, op)
    assert response["description"] == "pong"
Beispiel #4
0
def unmarshal_response_inner(response, op):
    """
    Unmarshal incoming http response into a value based on the
    response specification.
    :type response: :class:`bravado_core.response.IncomingResponse`
    :type op: :class:`bravado_core.operation.Operation`
    :returns: value where type(value) matches response_spec['schema']['type']
        if it exists, None otherwise.
    """
    deref = op.swagger_spec.deref
    response_spec = get_response_spec(status_code=response.status_code, op=op)

    if 'schema' not in response_spec:
        return None

    content_type = response.headers.get('content-type', '').lower()

    if content_type.startswith(APP_JSON) or content_type.startswith(APP_MSGPACK):
        content_spec = deref(response_spec['schema'])
        if content_type.startswith(APP_JSON):
            content_value = response.json()
        else:
            content_value = unpackb(response.raw_bytes, encoding='utf-8')

        if op.swagger_spec.config.get('validate_responses', False):
            validate_schema_object(op.swagger_spec, content_spec, content_value)

        return unmarshal_schema_object(
            swagger_spec=op.swagger_spec,
            schema_object_spec=content_spec,
            value=content_value,
        )

    # TODO: Non-json response contents
    return response.text
    def _marshal_object(self, request, response_object):
        # operation attribute is injected by validator_tween in case the endpoint is served by Swagger 2.0 specs
        operation = getattr(request, 'operation', None)

        if not operation:
            # If the request is not served by Swagger2.0 endpoint _marshal_object is NO_OP
            return response_object

        try:
            response_spec = get_response_spec(
                status_code=request.response.status_code,
                op=request.operation,
            )
            return marshal_schema_object(
                swagger_spec=request.registry.
                settings['pyramid_swagger.schema20'],
                schema_object_spec=response_spec['schema'],
                value=response_object,
            )
        except MatchingResponseNotFound:
            # Response specs not found
            return response_object
        except SwaggerMappingError:
            # marshaling process failed
            return response_object
Beispiel #6
0
def swaggerize_response(response, op):
    """
    Delegate handling the Swagger concerns of the response to bravado-core.

    :type response: :class:`pyramid.response.Response`
    :type op: :class:`bravado_core.operation.Operation`
    """
    response_spec = get_response_spec(response.status_int, op)
    bravado_core.response.validate_response(response_spec, op,
                                            PyramidSwaggerResponse(response))
def swaggerize_response(response, op):
    """
    Delegate handling the Swagger concerns of the response to bravado-core.

    :type response: :class:`pyramid.response.Response`
    :type op: :class:`bravado_core.operation.Operation`
    """
    response_spec = get_response_spec(response.status_int, op)
    bravado_core.response.validate_response(
        response_spec, op, PyramidSwaggerResponse(response))
Beispiel #8
0
def unmarshal_response_inner(
    response,  # type: IncomingResponse
    op,  # type: Operation
):
    # type: (...) -> typing.Optional[T]
    """
    Unmarshal incoming http response into a value based on the
    response specification.
    :type response: :class:`bravado_core.response.IncomingResponse`
    :type op: :class:`bravado_core.operation.Operation`
    :returns: value where type(value) matches response_spec['schema']['type']
        if it exists, None otherwise.
    """

    content_type = response.headers.get('content-type', '').lower()
    if content_type.startswith(APP_JSON) or content_type.startswith(APP_MSGPACK):
        use_models = op.swagger_spec.config.get('use_models', True)
        if content_type.startswith(APP_JSON):
            content_value = response.json()
        else:
            content_value = unpackb(response.raw_bytes)

        try:
            response_spec = get_response_spec(
                status_code=response.status_code, op=op)
        except MatchingResponseNotFound:
            if not use_models:
                return content_value

            six.reraise(*sys.exc_info())

        if 'schema' not in response_spec:
            if not use_models:
                return content_value

            return None

        content_spec = op.swagger_spec.deref(response_spec['schema'])
        if op.swagger_spec.config.get('validate_responses', False):
            validate_schema_object(op.swagger_spec, content_spec, content_value)

        return unmarshal_schema_object(
            swagger_spec=op.swagger_spec,
            schema_object_spec=content_spec,
            value=content_value,
        )

    if content_type.startswith('application'):
        return response.raw_bytes

    # TODO: Non-json response contents
    return response.text
Beispiel #9
0
def validate_response_dict(rule, response_dict, http_method='GET', status_code=200, headers=None):
    path = rule_to_path(rule)

    if not headers:
        headers = {}

    # TODO: Validate the entire response, including headers
    # response = Response(response_dict, headers)

    op = spec.get_op_for_request(http_method, path)
    response_spec = get_response_spec(status_code, op)

    # No schema defined? Nothing to validate.
    if 'schema' not in response_spec:
        return

    validate_schema_object(spec, response_spec['schema'], response_dict)
Beispiel #10
0
    def _marshal_object(self, request, response_object):
        # operation attribute is injected by validator_tween in case the endpoint is served by Swagger 2.0 specs
        operation = getattr(request, 'operation', None)

        if not operation:
            # If the request is not served by Swagger2.0 endpoint _marshal_object is NO_OP
            return response_object

        try:
            response_spec = get_response_spec(
                status_code=request.response.status_code,
                op=request.operation,
            )
            return marshal_schema_object(
                swagger_spec=request.registry.settings['pyramid_swagger.schema20'],
                schema_object_spec=response_spec['schema'],
                value=response_object,
            )
        except (MatchingResponseNotFound, SwaggerMappingError, KeyError):
            # marshaling process failed
            return response_object
Beispiel #11
0
def test_complicated_refs(simple_crossfer_spec):
    # Split the swagger spec into a bunch of different json files and use
    # $refs all over to place to wire stuff together - see the test-data
    # files or this will make no sense whatsoever.

    # Verify things are 'reachable' (hence, have been ingested correctly)

    # Resource
    assert simple_crossfer_spec.resources['pingpong']

    # Operation
    op = simple_crossfer_spec.resources['pingpong'].ping
    assert op

    # Parameter
    assert simple_crossfer_spec.resources['pingpong'].ping.params['pung']

    # Parameter name
    assert simple_crossfer_spec.resources['pingpong'].ping.params[
        'pung'].name == 'pung'

    # Response
    response = get_response_spec(200, op)
    assert response['description'] == 'pong'
Beispiel #12
0
def test_return_default_spec_when_no_match_on_status_code(
        empty_swagger_spec, op_spec):
    op = Operation(empty_swagger_spec, '/pet/{petId}', 'get', op_spec)
    assert op_spec['responses']['default'] == get_response_spec(404, op)
Beispiel #13
0
def test_return_spec_for_status_code(empty_swagger_spec, op_spec):
    op = Operation(empty_swagger_spec, '/pet/{petId}', 'get', op_spec)
    assert op_spec['responses']['200'] == get_response_spec(200, op)
Beispiel #14
0
 def _validate_response(swagger_op, result):
     response_spec = get_response_spec(int(response.status_code),
                                       swagger_op)
     outgoing_response = BottleOutgoingResponse(response, result)
     validate_response(response_spec, swagger_op, outgoing_response)
 def _validate_response(swagger_op, result):
     response_spec = get_response_spec(int(response.status_code), swagger_op)
     outgoing_response = BottleOutgoingResponse(response, result)
     validate_response(response_spec, swagger_op, outgoing_response)