Skip to content

Commit

Permalink
Merge pull request #3 from pachecoio/feature/readme
Browse files Browse the repository at this point in the history
update readme
  • Loading branch information
pachecoio authored May 2, 2024
2 parents 5a88657 + e97bddd commit 1089eaa
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 142 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "openapi-mocker"
version = "0.1.2"
version = "0.1.3"
edition = "2021"
repository = "https://github.com/pachecoio/openapi-mocker"
keywords = ["openapi", "mock", "mock-server"]
Expand Down
275 changes: 136 additions & 139 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,169 +99,166 @@ Custom examples can be defined and requested in different ways.

You can define an example with the exact path you want to match.
Example:

```yaml
openapi: 3.0.0
info:
title: Example API
version: 1.0.0
paths:
/hello/{name}:
get:
responses:
'200':
description: OK
content:
application/json:
schema:
type: object
properties:
message:
type: string
examples:
default:
value:
message: Hello, world!
/hello/jon_snow:
value:
message: You know nothing, Jon Snow!
```
```yaml
openapi: 3.0.0
info:
title: Example API
version: 1.0.0
paths:
/hello/{name}:
get:
responses:
'200':
description: OK
content:
application/json:
schema:
type: object
properties:
message:
type: string
examples:
default:
value:
message: Hello, world!
/hello/jon_snow:
value:
message: You know nothing, Jon Snow!
```

Request the example by the exact path:
```bash
curl -i http://localhost:8080/hello/jon_snow
```
The response should be:
```json
{"message":"You know nothing, Jon Snow!"}
```
```bash
curl -i http://localhost:8080/hello/jon_snow
```

The response should be:

```json
{"message":"You know nothing, Jon Snow!"}
```

Request the default example:
```bash
curl -i http://localhost:8080/hello/arya_stark
```
The response should be:
```json
{"message":"Hello, world!"}
```
```bash
curl -i http://localhost:8080/hello/arya_stark
```

The response should be:

```json
{"message":"Hello, world!"}
```

## Requesting by query parameter

You can define an example with a query parameter you want to match.

Example:

```yaml
openapi: 3.0.0
info:
title: Example API
version: 1.0.0
paths:
/hello:
get:
parameters:
- name: name
in: query
required: true
schema:
type: string
responses:
'200':
description: OK
content:
application/json:
schema:
type: object
properties:
message:
type: string
examples:
default:
value:
message: Hello, world!
"query:name=sansa":
value:
message: Sansa Stark
```
```yaml
openapi: 3.0.0
info:
title: Example API
version: 1.0.0
paths:
/hello:
get:
parameters:
- name: name
in: query
required: true
schema:
type: string
responses:
'200':
description: OK
content:
application/json:
schema:
type: object
properties:
message:
type: string
examples:
default:
value:
message: Hello, world!
"query:name=sansa":
value:
message: Sansa Stark
```

Request the example by the query parameter:
```bash
curl -i http://localhost:8080/hello?name=sansa
```
The response should be:
```json
{"message": "Sansa Stark"}
```
```bash
curl -i http://localhost:8080/hello?name=sansa
```
The response should be:
```json
{"message": "Sansa Stark"}
```

Request that does not match the query parameter:
```bash
curl -i http://localhost:8080/hello?name=arya
```
The response should be:
```json
{"message": "Hello, world!"}
```
```bash
curl -i http://localhost:8080/hello?name=arya
```
The response should be:
```json
{"message": "Hello, world!"}
```

## Requesting by headers

You can define an example with a header you want to match.

Example:

```yaml
openapi: 3.0.0
info:
title: Example API
version: 1.0.0
paths:
/hello:
get:
parameters:
- name: name
in: header
required: true
schema:
type: string
responses:
'200':
description: OK
content:
application/json:
schema:
type: object
properties:
message:
type: string
examples:
default:
value:
message: Hello, world!
"header:x-name=tyrion":
value:
message: Tyrion Lannister
```
```yaml
openapi: 3.0.0
info:
title: Example API
version: 1.0.0
paths:
/hello:
get:
parameters:
- name: name
in: header
required: true
schema:
type: string
responses:
'200':
description: OK
content:
application/json:
schema:
type: object
properties:
message:
type: string
examples:
default:
value:
message: Hello, world!
"header:x-name=tyrion":
value:
message: Tyrion Lannister
```

Request the example by the header:
```bash
curl -i http://localhost:8080/hello -H "x-name: tyrion"
```
The response should be:
```json
{"message": "Tyrion Lannister"}
```
```bash
curl -i http://localhost:8080/hello -H "x-name: tyrion"
```
The response should be:
```json
{"message": "Tyrion Lannister"}
```

Request that does not match the header:
```bash
curl -i http://localhost:8080/hello
```
The response should be:
```json
{"message": "Hello, world!"}
```
```bash
curl -i http://localhost:8080/hello
```
The response should be:
```json
{"message": "Hello, world!"}
```

> Note: The matches occur in the following order: path, query, headers.
> It is also important to note that the request is going to return the
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub mod openapi;
pub mod server;

#[derive(Parser)]
#[clap(version = "0.1.2", author = "Thiago Pacheco")]
#[clap(version = "0.1.3", author = "Thiago Pacheco")]
pub struct Args {
#[clap(index = 1)]
pub spec: PathBuf,
Expand Down

0 comments on commit 1089eaa

Please sign in to comment.