Skip to content

Commit

Permalink
Merge pull request #117 from xsnippet/get-snippet
Browse files Browse the repository at this point in the history
Implement the /snippets/<id> route
  • Loading branch information
ikalnytskyi committed Feb 3, 2021
2 parents 7d99315 + 150d73c commit 5c29968
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub fn create_app() -> Result<rocket::Rocket, Box<dyn Error>> {

let routes = routes![
routes::snippets::create_snippet,
routes::snippets::get_snippet,
routes::syntaxes::get_syntaxes,
];
Ok(app
Expand Down
5 changes: 5 additions & 0 deletions src/routes/snippets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,8 @@ pub fn create_snippet(

Ok(Created(location, Some(response)))
}

#[get("/snippets/<id>")]
pub fn get_snippet(storage: State<Box<dyn Storage>>, id: String) -> Result<JsonValue, ApiError> {
Ok(json!(storage.get(&id)?))
}
31 changes: 31 additions & 0 deletions tests/gabbits/get-snippet-errors.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
fixtures:
- XSnippetApi

tests:
- name: create a new snippet
POST: /v1/snippets
request_headers:
content-type: application/json
data:
title: Hello, World!
syntax: python
content: print('Hello, World!')
status: 201

- name: retrieve snippet (unsupported accept)
GET: $LOCATION
request_headers:
accept: text/html
response_headers:
content-type: application/json
status: 406
skip: content negotiation is not implemented yet

- name: retrieve snippet (not found)
GET: /v1/snippets/foobar
response_headers:
content-type: application/json
response_json_paths:
$.message: Snippet with id `foobar` is not found
$.`len`: 1
status: 404
54 changes: 54 additions & 0 deletions tests/gabbits/get-snippet.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
common:
- &datetime_regex /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+\+\d{2}:\d{2}$/
- &slug_regex /^[a-zA-Z0-9]+$/

fixtures:
- XSnippetApi

tests:
- name: create a new snippet
POST: /v1/snippets
request_headers:
content-type: application/json
data:
title: Hello, World!
syntax: python
content: print('Hello, World!')
tags:
- spam
- eggs
status: 201

- name: retrieve previously created snippet via Location header
GET: $LOCATION
response_headers:
content-type: application/json
response_json_paths:
$.id: *slug_regex
$.title: Hello, World!
$.syntax: python
$.content: print('Hello, World!')
$.created_at: *datetime_regex
$.updated_at: *datetime_regex
$.tags.`sorted`:
- eggs
- spam
$.`len`: 7
status: 200

- name: retrieve previously created snippet by ID
GET: /v1/snippets/$HISTORY['create a new snippet'].$RESPONSE['id']
response_headers:
content-type: application/json
response_json_paths:
$.id: *slug_regex
$.title: Hello, World!
$.syntax: python
$.content: print('Hello, World!')
$.created_at: *datetime_regex
$.updated_at: *datetime_regex
$.tags.`sorted`:
- eggs
- spam
$.`len`: 7
status: 200

0 comments on commit 5c29968

Please sign in to comment.