Skip to content
This repository has been archived by the owner on Apr 11, 2018. It is now read-only.

How do I mock the Post/Put/Delete data? #24

Open
feikiss opened this issue May 19, 2016 · 8 comments
Open

How do I mock the Post/Put/Delete data? #24

feikiss opened this issue May 19, 2016 · 8 comments

Comments

@feikiss
Copy link

feikiss commented May 19, 2016

In sample2, I can find the usage about how to prepare the Mock data for the Get request which seems very simple and easily. I want to know how can I prepare the mock data for POST/Put/Delete request in the same way with Sample 2?

like this:
var myData = [
{collection: '/pets', name: '/Lassie', data: {name: 'Lassie', type: 'dog1'}},
{collection: '/pets', name: '/Clifford', data: {name: 'Clifford', type: 'dog'}},
{collection: '/pets', name: '/Garfield', data: {name: 'Garfield', type: 'cat'}},
{collection: '/pets', name: '/Snoopy', data: {name: 'Snoopy', type: 'dog'}}
]
server.dataStore.save(Resource.parse(myData));

Any comment is appreciated!

Thanks,
Flyli

@JamesMessinger
Copy link
Owner

I'm not sure what you mean. Mock data isn't for a particular kind of request (get, post, put, etc.). It's just data. A GET request retrieves that data. A POST request inserts new data. A PUT request updates existing data. A DELETE request deletes existing data.

@feikiss
Copy link
Author

feikiss commented May 20, 2016

Thanks @BigstickCarpet for your comment. I mean in some special cases, I send a POST request, for example, I Post to create a new Pet, in the response, there is a field saying the status for this pet. I just care about the "status" field in the response. For this case, I want to mock this response and I don't care whether I can create the pet or not in the server. So, my question is :how can I mock this Post response in the swagger-server like we Mock the "GET" response?

@JamesMessinger
Copy link
Owner

If you want to return a different response than the default behavior, then you can add your own middleware to do so.

server.post('/pets/{petName}', function(req, res, next) {
  res.json({status: 'created successfully'});
}

@feikiss
Copy link
Author

feikiss commented May 24, 2016

Thanks @BigstickCarpet ! It works fine and I can mock the data what I want now. Just have one another question, I want to change the default behavior of swagger-server: If I send a 'get' request, and the response is empty, then I want to use the example value as the response. You know in the swagger-ui project, it can generate the example values via the swagger model definition, do you know how or where
should I change it please?

@JamesMessinger
Copy link
Owner

@feikiss - Glad to hear that it's working for you now. And yes, Swagger Server has built-in support for default and example responses. If you define a default or example response in your schema, then Swagger Server will automatically send that response if there's no data. See the end of the first walkthrough for an example

@feikiss
Copy link
Author

feikiss commented May 25, 2016

Yes @BigstickCarpet ,the default or example value in Swagger file can be used in Swagger Server, but it does not fit my requirement... I do not define the default value, but I want to send back the mock response via the schema definition...

For example,the following is the schema definition in the response.

"responses" : {
          "200" : {
            "description" : "successful operation",
            "schema" : {
              "$ref" : "#/definitions/ActionVO"
              }
            }
          }
}

"definitions" : {
    "ActionVO" : {
      "type" : "object",
      "properties" : {
        "id":{
            "type" : "string"
        }
        "name" : {
          "type" : "string"
        },
        "state" : {
          "type" : "string"
        }

and I'd like to send back the following value(also you can refer to the screen-shot attached) :

[
  {
    "id": "string",
    "name": "string",
    "state": "string"
  }
]

It is generated by the Swagger-UI, and I want to use it as the default value. Not sure whether we can integrated in the swagger-server, but I think there should be some possible way..

Thanks.

swagger example value

@JamesMessinger
Copy link
Owner

There are two ways that you can do this. The first, is to add the default to your schema:

"definitions" : {
    "ActionVO" : {
      "type" : "object",
      "properties" : {
        "id":{
            "type" : "string"
        },
        "name" : {
          "type" : "string"
        },
        "state" : {
          "type" : "string"
        },
      "default": {
          "id": "456XYZ",
          "name": "John Doe",
          "state": "California"
      }

The second is to write your own middleware to return whatever value you want:

server.post('/pets/{petName}', function(req, res, next) {
  res.json({
    id: "456XYZ",
    name: "John Doe",
    status: 'California'
  });
}

@feikiss
Copy link
Author

feikiss commented May 30, 2016

Thanks @BigstickCarpet so much, It works!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants