1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2025-12-17 17:47:03 +00:00

REST APIv1 now uses lowercase JSON property names

- Updated rest-apiv1.sh to pretty print JSON with jq if available
- Fixed some missing checks on JSON testutils
This commit is contained in:
James Hillyerd
2016-02-25 19:21:47 -08:00
parent 5da5d3e509
commit f36e21a65c
4 changed files with 76 additions and 58 deletions

View File

@@ -36,6 +36,7 @@ arg_check() {
main() { main() {
# Process options # Process options
local curl_opts="" local curl_opts=""
local pretty="true"
for arg in $*; do for arg in $*; do
if [[ $arg == -* ]]; then if [[ $arg == -* ]]; then
case "$arg" in case "$arg" in
@@ -45,6 +46,7 @@ main() {
;; ;;
-i) -i)
curl_opts="$curl_opts -i" curl_opts="$curl_opts -i"
pretty=""
;; ;;
**) **)
echo "Unknown option: $arg" >&2 echo "Unknown option: $arg" >&2
@@ -65,11 +67,13 @@ main() {
local url="" local url=""
local method="GET" local method="GET"
local is_json=""
case "$command" in case "$command" in
body) body)
arg_check "$command" 2 $# arg_check "$command" 2 $#
url="$URL_ROOT/mailbox/$1/$2" url="$URL_ROOT/mailbox/$1/$2"
is_json="true"
;; ;;
delete) delete)
arg_check "$command" 2 $# arg_check "$command" 2 $#
@@ -79,6 +83,7 @@ main() {
list) list)
arg_check "$command" 1 $# arg_check "$command" 1 $#
url="$URL_ROOT/mailbox/$1" url="$URL_ROOT/mailbox/$1"
is_json="true"
;; ;;
purge) purge)
arg_check "$command" 1 $# arg_check "$command" 1 $#
@@ -97,7 +102,12 @@ main() {
;; ;;
esac esac
curl $curl_opts -H "Accept: application/json" --noproxy "$API_HOST" -X "$method" "$url" # Use jq to pretty-print if installed and we are expecting JSON output
if [ $pretty ] && [ $is_json ] && type -P jq; then
curl -s $curl_opts -H "Accept: application/json" --noproxy "$API_HOST" -X "$method" "$url" | jq .
else
curl -s $curl_opts -H "Accept: application/json" --noproxy "$API_HOST" -X "$method" "$url"
fi
} }
if [ $# -lt 1 ]; then if [ $# -lt 1 ]; then

View File

@@ -14,30 +14,30 @@ import (
// JSONMessageHeaderV1 contains the basic header data for a message // JSONMessageHeaderV1 contains the basic header data for a message
type JSONMessageHeaderV1 struct { type JSONMessageHeaderV1 struct {
Mailbox string Mailbox string `json:"mailbox"`
ID string `json:"Id"` ID string `json:"id"`
From string From string `json:"from"`
Subject string Subject string `json:"subject"`
Date time.Time Date time.Time `json:"date"`
Size int64 Size int64 `json:"size"`
} }
// JSONMessageV1 contains the same data as the header plus a JSONMessageBody // JSONMessageV1 contains the same data as the header plus a JSONMessageBody
type JSONMessageV1 struct { type JSONMessageV1 struct {
Mailbox string Mailbox string `json:"mailbox"`
ID string `json:"Id"` ID string `json:"id"`
From string From string `json:"from"`
Subject string Subject string `json:"subject"`
Date time.Time Date time.Time `json:"date"`
Size int64 Size int64 `json:"size"`
Body *JSONMessageBodyV1 Body *JSONMessageBodyV1 `json:"body"`
Header mail.Header Header mail.Header `json:"header"`
} }
// JSONMessageBodyV1 contains the Text and HTML versions of the message body // JSONMessageBodyV1 contains the Text and HTML versions of the message body
type JSONMessageBodyV1 struct { type JSONMessageBodyV1 struct {
Text string Text string `json:"text"`
HTML string `json:"Html"` HTML string `json:"html"`
} }
// MailboxListV1 renders a list of messages in a mailbox // MailboxListV1 renders a list of messages in a mailbox

View File

@@ -16,16 +16,16 @@ const (
baseURL = "http://localhost/api/v1" baseURL = "http://localhost/api/v1"
// JSON map keys // JSON map keys
mailboxKey = "Mailbox" mailboxKey = "mailbox"
idKey = "Id" idKey = "id"
fromKey = "From" fromKey = "from"
subjectKey = "Subject" subjectKey = "subject"
dateKey = "Date" dateKey = "date"
sizeKey = "Size" sizeKey = "size"
headerKey = "Header" headerKey = "header"
bodyKey = "Body" bodyKey = "body"
textKey = "Text" textKey = "text"
htmlKey = "Html" htmlKey = "html"
) )
func TestRestMailboxList(t *testing.T) { func TestRestMailboxList(t *testing.T) {

View File

@@ -102,7 +102,8 @@ func (d *InputMessageData) CompareToJSONMessageMap(json interface{}) (errors []s
if m, ok := json.(map[string]interface{}); ok { if m, ok := json.(map[string]interface{}); ok {
// Get nested body map // Get nested body map
if body := m[bodyKey].(map[string]interface{}); ok { if m[bodyKey] != nil {
if body, ok := m[bodyKey].(map[string]interface{}); ok {
if msg, ok := isJSONStringEqual(textKey, d.Text, body[textKey]); !ok { if msg, ok := isJSONStringEqual(textKey, d.Text, body[textKey]); !ok {
errors = append(errors, msg) errors = append(errors, msg)
} }
@@ -113,6 +114,9 @@ func (d *InputMessageData) CompareToJSONMessageMap(json interface{}) (errors []s
panic(fmt.Sprintf("Expected map[string]interface{} in json key %q, got %T", panic(fmt.Sprintf("Expected map[string]interface{} in json key %q, got %T",
bodyKey, m[bodyKey])) bodyKey, m[bodyKey]))
} }
} else {
errors = append(errors, fmt.Sprintf("Expected body in JSON %q but it was nil", bodyKey))
}
exDate := d.Date.Format("2006-01-02T15:04:05.999999999-07:00") exDate := d.Date.Format("2006-01-02T15:04:05.999999999-07:00")
if msg, ok := isJSONStringEqual(dateKey, exDate, m[dateKey]); !ok { if msg, ok := isJSONStringEqual(dateKey, exDate, m[dateKey]); !ok {
errors = append(errors, msg) errors = append(errors, msg)
@@ -122,7 +126,8 @@ func (d *InputMessageData) CompareToJSONMessageMap(json interface{}) (errors []s
} }
// Get nested header map // Get nested header map
if header := m[headerKey].(map[string]interface{}); ok { if m[headerKey] != nil {
if header, ok := m[headerKey].(map[string]interface{}); ok {
// Loop over input (expected) header names // Loop over input (expected) header names
for name, keyInputHeaders := range d.Header { for name, keyInputHeaders := range d.Header {
// Make sure expected header name exists in received JSON // Make sure expected header name exists in received JSON
@@ -153,6 +158,9 @@ func (d *InputMessageData) CompareToJSONMessageMap(json interface{}) (errors []s
} }
} }
} }
} else {
errors = append(errors, fmt.Sprintf("Expected header in JSON %q but it was nil", headerKey))
}
} else { } else {
panic(fmt.Sprintf("Expected map[string]interface{} in json, got %T", json)) panic(fmt.Sprintf("Expected map[string]interface{} in json, got %T", json))
} }