eykrehbein / strest

⚡️ CI-ready tests for REST APIs configured in YAML
MIT License
1.74k stars 59 forks source link

add support to read request data from json file #146

Closed dungahk closed 4 years ago

dungahk commented 4 years ago

What does this Pull Request do?

It adds support to read from JSON files to fill the request body.

Use case

Sometimes, the request body is just massive and adding it to the request using the text attribute is out of option. Having the ability to read from a JSON file makes it a lot easier to control the strest test file.

Other

This fixes an issue with the moment tests that was failing as well because of the new year.

jgroom33 commented 4 years ago

Thanks for the contribution!

Can you look into these requested changes:

the file upload should follow the har format. http://www.softwareishard.com/blog/har-12-spec/#params

Note that text and params fields are mutually exclusive.

version: 2
requests:
  set_JsonPath:
    request:
      url: https://jsonplaceholder.typicode.com/posts
      method: POST
      postData:
        mimeType: application/json
        params:
        - name: file.json # probably not used
          fileName: local/path/to/file.json
          contentType: application/json
dungahk commented 4 years ago

Thanks for the response @jgroom33 but my use case is not about uploading files. I want to send a POST request with JSON in the body, but I do not want to put the body in the strest.yml file because the request body is huge and that would make it totally unmaintainable!

Unless I haven't understood the usage correctly, there is no way in strest to do that currently, is that incorrect?

jgroom33 commented 4 years ago

It looks like it might be supported already. Have you tried this in the text field: https://github.com/eykrehbein/strest#sending-files-and-form-data

On Mon, Jan 20, 2020, 3:26 PM Emerson Jair notifications@github.com wrote:

Thanks for the response @jgroom33 https://github.com/jgroom33 but my use case is not about uploading files. I want to send a POST request with JSON in the body, but I do not want to put the body in the strest.yml file because the request body is huge and that would make it totally unmaintainable!

Unless I haven't understood the usage correctly, there is no way in strest to do that currently, is that incorrect?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/eykrehbein/strest/pull/146?email_source=notifications&email_token=AA6JMDWLDZDKVFF565GDGX3Q6YQHZA5CNFSM4KI5G5I2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEJN6O6Y#issuecomment-576448379, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA6JMDQ273S5WVGNESMSMSDQ6YQHZANCNFSM4KI5G5IQ .

dungahk commented 4 years ago

Hi @jgroom33 that still does not work for me, I do not have a parameter I want to pass a file to it, the file is the whole body of my request.

Let's say I want to make a request to an API and send the following data:

{
  "foo": 1,
  "bar": {
    "foo1": 2,
    "foo2": 3
  }
}

Now, instead of writing a strest file like the following:

version: 2

requests:
    fooBar:
      request:
        url: https://anyapi.com/api/v1/post
        method: POST
        postData:
          mimeType: application/json
          text:
            foo: 1
            bar:
              foo1: 2
              foo2: 3

I want to write it like this:

version: 2

requests:
    fooBar:
      request:
        url: https://anyapi.com/api/v1/post
        method: POST
        json: tests/data.json

Strest will read that JSON file and add the data to the request body and send that to the target url.

jgroom33 commented 4 years ago

ok... its somewhat supported like this:

file1:

version: 2

variables:
  baseUrl: https://postman-echo.com
  exampleData: {
    "foo": "bar"
  }

file2:

version: 2

requests:
  postRequest:
    request:
      url: <$ baseUrl $>/post
      method: POST
      postData:
        mimeType: text/plain
        text: <$ exampleData | dump | safe $>
    validate:
    - jsonpath: status
      expect: 200
eykrehbein commented 4 years ago

I can definitely understand your issue and your solution is good. Going to approve it after you've fixed these minor discussions :+1:

dungahk commented 4 years ago

Thank you @eykrehbein , I will check them as soon as possible. Should be done by the end of the weekend.

dungahk commented 4 years ago

@eykrehbein I've done the requested changes.

dungahk commented 4 years ago

If you need me to rebase/squash or something like that, let me know.