Skip to content

Commit

Permalink
serving json
Browse files Browse the repository at this point in the history
  • Loading branch information
ericminio committed Apr 26, 2024
1 parent 578bc83 commit 0815cae
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 15 deletions.
9 changes: 5 additions & 4 deletions stub-json/about/tests.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
#!/bin/bash

source ../support/nginx/reload.sh
source ../support/trim.sh
source ../support/utils.sh

function test_json_header {
nginx_reload_configuration
type=$(curl --head http://localhost:8080/json | grep "Content-Type:" | trim)
type=$(curl --head http://localhost:8080/ping | grep "Content-Type:" | oneliner)

assertequals "$type" "Content-Type: application/json"
}

function test_json_content {
nginx_reload_configuration
body=$(curl http://localhost:8080/json)
body=$(curl http://localhost:8080/alive | json)

assertequals "$body" "{\"alive\":true}"

assertequals "$body" '{ "alive":true, "value":42 }'
}
1 change: 1 addition & 0 deletions stub-json/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ services:
- 8080:80
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d
- ./nginx/data:/etc/nginx/data
10 changes: 5 additions & 5 deletions stub-json/nginx/conf.d/default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ map $request_body $matching {
}

server {
default_type text/plain;
default_type application/json;
root /etc/nginx/data;

location /ping {
return 200 'pong';
return 200 '{"message":"pong"}';
}

location /json {
default_type application/json;
return 200 '{"alive":true}';
location /alive {
try_files /alive.json =404;
}

}
4 changes: 4 additions & 0 deletions stub-json/nginx/data/alive.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"alive": true,
"value": 42
}
2 changes: 1 addition & 1 deletion support/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function assertequals {
if [ "$1" = "$2" ]; then
return 0
else
FAILED_EXPECTATION="FAILURE\n\nExpected: $2 \nBut was : $1"
FAILED_EXPECTATION="\nFAILURE\nExpected: $2 \nBut was : $1"
return 1
fi
}
Expand Down
5 changes: 0 additions & 5 deletions support/trim.sh

This file was deleted.

14 changes: 14 additions & 0 deletions support/utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

function oneliner {
tr -d '\n' | tr -d '\r'
}

function compact {
oneliner | tr -d ' '
}

function json {
compact | sed 's/{/{ /g' | sed 's/}/ }/g' | sed 's/,/, /g'
}

0 comments on commit 0815cae

Please sign in to comment.