mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-18 01:57:02 +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:
@@ -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
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
|||||||
@@ -102,16 +102,20 @@ 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 msg, ok := isJSONStringEqual(textKey, d.Text, body[textKey]); !ok {
|
if body, ok := m[bodyKey].(map[string]interface{}); ok {
|
||||||
errors = append(errors, msg)
|
if msg, ok := isJSONStringEqual(textKey, d.Text, body[textKey]); !ok {
|
||||||
}
|
errors = append(errors, msg)
|
||||||
if msg, ok := isJSONStringEqual(htmlKey, d.HTML, body[htmlKey]); !ok {
|
}
|
||||||
errors = append(errors, msg)
|
if msg, ok := isJSONStringEqual(htmlKey, d.HTML, body[htmlKey]); !ok {
|
||||||
|
errors = append(errors, msg)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
panic(fmt.Sprintf("Expected map[string]interface{} in json key %q, got %T",
|
||||||
|
bodyKey, m[bodyKey]))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
panic(fmt.Sprintf("Expected map[string]interface{} in json key %q, got %T",
|
errors = append(errors, fmt.Sprintf("Expected body in JSON %q but it was nil", bodyKey))
|
||||||
bodyKey, m[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 {
|
||||||
@@ -122,36 +126,40 @@ 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 {
|
||||||
// Loop over input (expected) header names
|
if header, ok := m[headerKey].(map[string]interface{}); ok {
|
||||||
for name, keyInputHeaders := range d.Header {
|
// Loop over input (expected) header names
|
||||||
// Make sure expected header name exists in received JSON
|
for name, keyInputHeaders := range d.Header {
|
||||||
if keyOutputVals, ok := header[name]; ok {
|
// Make sure expected header name exists in received JSON
|
||||||
if keyOutputHeaders, ok := keyOutputVals.([]interface{}); ok {
|
if keyOutputVals, ok := header[name]; ok {
|
||||||
// Loop over input (expected) header values
|
if keyOutputHeaders, ok := keyOutputVals.([]interface{}); ok {
|
||||||
for _, inputHeader := range keyInputHeaders {
|
// Loop over input (expected) header values
|
||||||
hasValue := false
|
for _, inputHeader := range keyInputHeaders {
|
||||||
// Look for expected value in received headers
|
hasValue := false
|
||||||
for _, outputHeader := range keyOutputHeaders {
|
// Look for expected value in received headers
|
||||||
if inputHeader == outputHeader {
|
for _, outputHeader := range keyOutputHeaders {
|
||||||
hasValue = true
|
if inputHeader == outputHeader {
|
||||||
break
|
hasValue = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !hasValue {
|
||||||
|
errors = append(errors, fmt.Sprintf(
|
||||||
|
"JSON %v[%q] missing value %q", headerKey, name, inputHeader))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !hasValue {
|
} else {
|
||||||
errors = append(errors, fmt.Sprintf(
|
// keyOutputValues was not a slice of interface{}
|
||||||
"JSON %v[%q] missing value %q", headerKey, name, inputHeader))
|
panic(fmt.Sprintf("Expected []interface{} in %v[%q], got %T", headerKey,
|
||||||
}
|
name, keyOutputVals))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// keyOutputValues was not a slice of interface{}
|
errors = append(errors, fmt.Sprintf("JSON %v missing key %q", headerKey, name))
|
||||||
panic(fmt.Sprintf("Expected []interface{} in %v[%q], got %T", headerKey,
|
|
||||||
name, keyOutputVals))
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
errors = append(errors, fmt.Sprintf("JSON %v missing key %q", headerKey, name))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} 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))
|
||||||
|
|||||||
Reference in New Issue
Block a user