open-api-mocker icon indicating copy to clipboard operation
open-api-mocker copied to clipboard

Will the mock response support octet-stream?

Open Celthi opened this issue 4 years ago • 8 comments

I have an endpoint that will return a octet-stream as follows, but the response will be json instead of octet-stream

      responses:
        "200":
          description: The requested properties are successfully returned.
          content:
            application/octet-stream:
              schema:
                type: string
                format: binary
                x-content-type: application/octet-stream
                example:   # Sample object
                  externalValue: '/Users/xxx/Downloads/nodejs-server-mock2/README.md'

With the above schema, the response is

curl -v -X GET "http://localhost:5000/api/objects" -H "accept: application/octet-stream" -H "X: fasdfa"
Note: Unnecessary use of -X or --request, GET is already inferred.
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 5000 (#0)
> GET /api/objects HTTP/1.1
> Host: localhost:5000
> User-Agent: curl/7.64.1
> accept: application/octet-stream
> X-MSTR-ISERVER-SESSION: fasdfa
> 
< HTTP/1.1 200 OK
< x-powered-by: jormaechea/open-api-mock
< Vary: Origin
< Access-Control-Allow-Credentials: true
< Content-Type: application/json; charset=utf-8
< Content-Length: 72
< ETag: W/"48-moCYJiPDoNqlzQ7AhOmaHZSX3cU"
< Date: Tue, 20 Apr 2021 00:52:10 GMT
< Connection: keep-alive
< 
* Connection #0 to host localhost left intact
{"externalValue":"/Users/xxx/Downloads/nodejs-server-mock2/README.md"}* Closing connection 0

Celthi avatar Apr 20 '21 00:04 Celthi

If I commented out the example above, the response will be

* Connected to localhost (::1) port 5000 (#0)
> GET /api/objects HTTP/1.1
> Host: localhost:5000
> User-Agent: curl/7.64.1
> accept: application/octet-stream
> X-MSTR-ISERVER-SESSION: fasdfa
> 
< HTTP/1.1 200 OK
< x-powered-by: jormaechea/open-api-mock
< Vary: Origin
< Access-Control-Allow-Credentials: true
< Content-Type: application/json; charset=utf-8
< Content-Length: 8
< ETag: W/"8-r11mbMxk38LLcw6NaqQZHEqTW1U"
< Date: Tue, 20 Apr 2021 01:11:40 GMT
< Connection: keep-alive
< 
* Connection #0 to host localhost left intact
"string"* Closing connection 0

Celthi avatar Apr 20 '21 01:04 Celthi

So both will return < Content-Type: application/json; charset=utf-8 instead of octet-stream

Celthi avatar Apr 20 '21 12:04 Celthi

I think we have two problems here:

  • The externalValue property is not supported yet. If you want that, I recommend to open a separate issue for it.
  • We're not handling the response content-type properly.

For the second problem I think that we have to make two improvements:

  1. Get the media-type from the response object and format the API response according to it.
  2. Add support for the Accept HTTP header to select the proper response (or respond with a 415 Unsupported Media Type if it's not defined in the schema)

Relevant documentations:

jormaechea avatar Apr 21 '21 00:04 jormaechea

Thanks. I'll try to see if I can make the enhancement

Celthi avatar Apr 21 '21 00:04 Celthi

Hi @Celthi any updates on this? If you don't have time for it maybe someone else can take care.. Just let me know 😉

jormaechea avatar Apr 29 '21 00:04 jormaechea

@jormaechea Sorry for the late reply. Currently, I don't have time to implement that. Free feel to assign to other people that are interested. And the stream might be better to work with implementing the exteranlValue support as it is better to provide a customized stream instead of random binary. I'll create another issue to track the exteranlValue support

Celthi avatar May 03 '21 09:05 Celthi

@jormaechea It seems there is a workaround by specifying a header "content-type: application/octet-stream` in the response like

      responses:
        "200":
          description: The requested properties are successfully returned.
          content:
            application/octet-stream:
              schema:
                type: string
                format: binary
                example: 3456
          headers:
            content-type:
              schema:
                type:
                  string
              example: application/octet-stream

After providing the header in the response, the browser will treat the response as a file stream though the data is actually a string named "3456"

Celthi avatar May 03 '21 09:05 Celthi

It's a great workaround. Definitely not the best solution, but it works for you.

I'll leave this issue open to implement a more elegant solution to handle response content-types

jormaechea avatar May 07 '21 04:05 jormaechea