Aconex / drakov

Mock Server that implements the API Blueprint specification

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Problem getting different responses with "Prefer: status=xxx" header

khaliddermoumi opened this issue · comments

I have a problem getting different responses using the "Prefer"-header.

In the FAQ, it says: "You can request a specific response by adding a Prefer header to the request in the form Prefer:status=XXX where XXX is the status code of the desired response."
I have tried that and can't get it to work.
I have a blueprint with a PUT-action, that has 2 responses defined:

### Update Something[PUT]
Updates something.

+ Request (application/json)

    + Headers

            Authorization: Bearer e2Rlc2NyaXB0aW9uOiB0aGlzIGlzIGFuIGFjY2VzcyB0b2tlbn0=

    + Body

            {
                "uniqueId": "ec58279d79f242189d153f575a8b9df0",
                "timestamp": "2015-04-06T10:01:12",
                "subject": "ACME Corp."
            }

+ Response 200 (application/vnd.siren+json)

        {
            "class": [ "something" ],
            "properties": {
                "uniqueId": "ec58279d79f242189d153f575a8b9df0",
                "timestamp": "2015-04-06T10:01:12",
                "subject": "ACME Corp."
            },
            "links": [
                { "rel": [ "self" ], "href": "https://localhost:3000/something/ec58279d79f242189d153f575a8b9df0" }
            ],
            "actions": [
                { "name": "update-something", "title": "Update Something", "method": "PUT", "href": "https://localhost:3000/something/ec58279d79f242189d153f575a8b9df0" }
            ]
        }

+ Response 409 (application/problem+json)

        {
            "type": "some error",
            "title": "There is an update conflict",
            "detail": "The resource could not be updated, because it is based on an older version that was updated meanwhile. Get the new version, merge your data, and retry the update."
        }

Now, when I call using curl, drakov properly returns the "200" response (as expected):

curl --cacert certificates/ia.crt \
    --header "Authorization: Bearer e2Rlc2NyaXB0aW9uOiB0aGlzIGlzIGFuIGFjY2VzcyB0b2tlbn0=" \
    --header "Content-Type: application/json" \
    --request PUT \
    --data '{
                "uniqueId": "ec58279d79f242189d153f575a8b9df0",
                "timestamp": "2015-04-06T10:01:12",
                "subject": "ACME Corp."
            }' \
    https://localhost:3000/something/ec58279d79f242189d153f575a8b9df0

But, when I add the "Prefer"-header (as documented), drakov does not return the "409" response, but says "cannot PUT":

curl --cacert certificates/ia.crt \
    --header "Authorization: Bearer e2Rlc2NyaXB0aW9uOiB0aGlzIGlzIGFuIGFjY2VzcyB0b2tlbn0=" \
    --header "Content-Type: application/json" \
    --header "Prefer:status=409" \
    --request PUT \
    --data '{
                "uniqueId": "ec58279d79f242189d153f575a8b9df0",
                "timestamp": "2015-04-06T10:01:12",
                "subject": "ACME Corp."
            }' \
    https://localhost:3000/something/ec58279d79f242189d153f575a8b9df0

-> Response is:
Cannot PUT /something/ec58279d79f242189d153f575a8b9df0

Any hints what is wrong?

BTW: thx a lot for drakov!

Regards,
Khalid

hey @khaliddermoumi thanks for your question, I'll have a look at this over the next day and get back to you 👍

I can see while debugging that the content type isn't being matched for your 409 prefer header request.

I'll investigate this and hopefully get a fix in for tomorrow 👍

Thanks again for reporting this

Thanks Yakov!

OK so, I've spent some time looking at this, what Drakov expects is a collection of request/response pairs.

Since your 409 doesn't have a request, there's no content-type, headers or schema/body to make that pair a match.

You could either copy the request into the 409 section or use a schema like so:

FORMAT: 1A

# Return something
Do something

## Something [/something]

### Update Something[PUT]
Updates something.

+ Request (application/json)

    + Headers

            Authorization: Bearer e2Rlc2NyaXB0aW9uOiB0aGlzIGlzIGFuIGFjY2VzcyB0b2tlbn0=

    + Body

            {
                "uniqueId": "ec58279d79f242189d153f575a8b9df0",
                "timestamp": "2015-04-06T10:01:12",
                "subject": "ACME Corp."
            }

+ Response 200 (application/vnd.siren+json)

        {
            "class": [ "something" ],
            "properties": {
                "uniqueId": "ec58279d79f242189d153f575a8b9df0",
                "timestamp": "2015-04-06T10:01:12",
                "subject": "ACME Corp."
            },
            "links": [
                { "rel": [ "self" ], "href": "https://localhost:3000/something/ec58279d79f242189d153f575a8b9df0" }
            ],
            "actions": [
                { "name": "update-something", "title": "Update Something", "method": "PUT", "href": "https://localhost:3000/something/ec58279d79f242189d153f575a8b9df0" }
            ]
        }

+ Request (application/json)

    + Headers

            Authorization: Bearer e2Rlc2NyaXB0aW9uOiB0aGlzIGlzIGFuIGFjY2VzcyB0b2tlbn0=

    + Schema
            {
                "type": "object",
                "properties": {
                    "uniqueId": {"type": "string"},
                    "timestamp": {"type": "string" },
                    "subject": {"type": "string" }
                }
            }

+ Response 409 (application/problem+json)

        {
            "type": "some error",
            "title": "There is an update conflict",
            "detail": "The resource could not be updated, because it is based on an older version that was updated meanwhile. Get the new version, merge your data, and retry the update."
        }

Thanks for your investigation, Yakov!