Fix tests and MIME attachments

This commit is contained in:
Konstantin Nazarov 2023-10-01 15:22:44 +01:00
parent e279f66801
commit 87c286d5b1
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
2 changed files with 27 additions and 27 deletions

View file

@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
#
# Distributed under the terms of the BSD License
#
@ -121,7 +121,7 @@ get_headers() {
FILTER="\
{ \
if (\$0~/^$/) {exit} \
if (\$0!~/^[^ ]*: .*$/) {exit} \
if (\$0!~/^[^ ]*:.*$/) {exit} \
print \$0 \
} \
"
@ -276,7 +276,7 @@ pack_part() {
echo "Content-Type: $CONTENT_TYPE"
echo "Content-Transfer-Encoding: base64"
echo
base64 | fold -w 76 < "$PART_FILE"
base64 < "$PART_FILE" | fold -w 76
fi
}

48
test.sh
View file

@ -1,16 +1,16 @@
#!/bin/bash
#!/usr/bin/env bash
#
# Distributed under the terms of the BSD License
#
# Copyright (c) 2021, Konstantin Nazarov <mail@knazarov.com>
# All rights reserved.
#
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
@ -61,10 +61,10 @@ assert() {
command="$1"
expected="$(echo -ne "${2:-}")"
result="$(eval 2>/dev/null $1 <<< ${3:-})" || true
if [[ "$result" == "$expected" ]]; then
return
fi
return
fi
result="$(sed -e :a -e '$!N;s/\n/\\n/;ta' <<< "$result")"
echo "Expected '$command' == '$expected'. Got: '$result'"
@ -97,7 +97,7 @@ new_note_from_file() {
"$BASE_DIR/notes.sh" -n "$TMP/input.md"
OUTPUT="$(cat "$(pwd)/notes/cur"/*)"
assert 'echo "$OUTPUT" | grep Subject' "Subject: This is a header"
}
@ -111,7 +111,7 @@ new_note_from_dir() {
"$BASE_DIR/notes.sh" -n "$TMP/inpdir"
OUTPUT="$(cat "$(pwd)/notes/cur"/*)"
assert 'echo "$OUTPUT" | grep Subject' "Subject: This is a header"
}
@ -169,7 +169,7 @@ edit_note() {
NOTE_ID="$(cat "$(pwd)/notes/cur"/* | grep X-Note-Id | cut -d ' ' -f 2)"
cat > "$(pwd)/editor.sh" <<- EOF
#!/bin/bash
#!/usr/bin/env bash
FILENAME="\$1"
echo "line2" >> "\$FILENAME"
EOF
@ -192,7 +192,7 @@ edit_note_add_file() {
NOTE_ID="$(cat "$(pwd)/notes/cur"/* | grep X-Note-Id | cut -d ' ' -f 2)"
cat > "$(pwd)/editor.sh" <<- EOF
#!/bin/bash
#!/usr/bin/env bash
FILENAME="\$1"
echo "newfile" > "\$FILENAME.txt"
EOF
@ -216,7 +216,7 @@ edit_note_no_modifications() {
NOTE_FILE="$(ls "$(pwd)/notes/cur"/*)"
cat > "$(pwd)/editor.sh" <<- EOF
#!/bin/bash
#!/usr/bin/env bash
EOF
chmod a+x "$(pwd)/editor.sh"
export EDITOR="$(pwd)/editor.sh"
@ -233,7 +233,7 @@ resume_editing() {
NOTE_ID="$(cat "$(pwd)/notes/cur"/* | grep X-Note-Id | cut -d ' ' -f 2)"
cat > "$(pwd)/editor.sh" <<- EOF
#!/bin/bash
#!/usr/bin/env bash
FILENAME="\$1"
echo "myline2" >> "\$FILENAME"
exit 1
@ -247,7 +247,7 @@ resume_editing() {
assert 'echo "$OUTPUT" | grep myline | wc -l | tr -d " "' "1"
cat > "$(pwd)/editor.sh" <<- EOF
#!/bin/bash
#!/usr/bin/env bash
FILENAME="\$1"
echo "myline3" >> "\$FILENAME"
EOF
@ -277,7 +277,7 @@ pack_multipart() {
"$BASE_DIR/notes.sh" -n "$TMP/inpdir"
OUTPUT="$(cat "$(pwd)/notes/cur"/*)"
assert 'echo "$OUTPUT" | grep Subject' "Subject: This is a header"
assert 'echo "$OUTPUT" | grep -o "text attachment"' "text attachment"
BOUNDARY="$(cat "$(pwd)/notes/cur"/* | grep boundary= | cut -d '=' -f 2 | tr -d '\"')"
@ -285,21 +285,21 @@ pack_multipart() {
OUTPUT="$(echo "$OUTPUT" | sed "s/$BOUNDARY/boundary/g")"
OUTPUT="$(echo "$OUTPUT" | grep -v "Date" | grep -v "X-Note-Id")"
read -d '' -r EXPECTED <<- EOF || true
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="boundary"
Subject: This is a header
--boundary
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline
This is a body
--boundary
Content-Disposition: attachment; filename="file.txt"
Content-Type: text/plain
This is a text attachment
--boundary--
EOF
@ -319,7 +319,7 @@ pack_multipart_binary() {
"$BASE_DIR/notes.sh" -n "$TMP/inpdir"
OUTPUT="$(cat "$(pwd)/notes/cur"/*)"
assert 'echo "$OUTPUT" | grep Subject' "Subject: This is a header"
BOUNDARY="$(cat "$(pwd)/notes/cur"/* | grep boundary= | cut -d '=' -f 2 | tr -d '\"')"
@ -327,16 +327,16 @@ pack_multipart_binary() {
OUTPUT="$(echo "$OUTPUT" | sed "s/$BOUNDARY/boundary/g")"
OUTPUT="$(echo "$OUTPUT" | grep -v "Date" | grep -v "X-Note-Id")"
read -d '' -r EXPECTED <<- EOF || true
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="boundary"
Subject: This is a header
--boundary
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline
This is a body
--boundary
Content-Disposition: attachment; filename="file.png"
@ -367,7 +367,7 @@ existing_headers() {
"$BASE_DIR/notes.sh" -n "$TMP/inpdir"
OUTPUT="$(cat "$(pwd)/notes/cur"/*)"
assert 'echo "$OUTPUT" | grep Subject' "Subject: This is a subject"
assert 'echo "$OUTPUT" | grep X-Note-Id' "X-Note-Id: $NOTE_ID"