add more args for http container test

Signed-off-by: jffarge <slumbering.pierrot@gmail.com>
This commit is contained in:
jffarge 2021-10-22 16:13:55 +02:00 committed by guillaume
parent b94b8c0c70
commit 04781f4e07
2 changed files with 19 additions and 21 deletions

View File

@ -2,7 +2,7 @@ package http
import ( import (
"encoding/json" "encoding/json"
"strconv" // "strconv"
"alpha.dagger.io/alpine" "alpha.dagger.io/alpine"
"alpha.dagger.io/dagger" "alpha.dagger.io/dagger"
@ -32,6 +32,7 @@ import (
shell: path: "/bin/bash" shell: path: "/bin/bash"
always: true always: true
env: { env: {
METHOD: method METHOD: method
HEADERS: json.Marshal(request.header) HEADERS: json.Marshal(request.header)
@ -47,15 +48,15 @@ import (
-L --fail --silent --show-error -L --fail --silent --show-error
--write-out "%{http_code}" --write-out "%{http_code}"
-X "$METHOD" -X "$METHOD"
-d "$BODY"
-o /response -o /response
) )
[ -n "$BODY" ] && curlArgs+=("-d" "'$BODY'")
headers="$(echo $HEADERS | jq -r 'to_entries | map(.key + ": " + (.value | tostring) + "\n") | add')" headers="$(echo $HEADERS | jq -r 'to_entries | map(.key + ": " + (.value | tostring) + "\n") | add')"
while read h; do while read h; do
curlArgs+=("-H" "$h") curlArgs+=("-H" "'$h'")
done <<< "$headers" done <<< "$headers"
if [ -e /token ]; then if [ -e /token ]; then
curlArgs+=("-H" "Authorization: bearer $(cat /token)") curlArgs+=("-H" "Authorization: bearer $(cat /token)")
fi fi
@ -71,11 +72,11 @@ import (
path: "/response" path: "/response"
} }
}.contents @dagger(output) }.contents @dagger(output)
statusCode: strconv.Atoi({ statusCode: {
os.#File & { os.#File & {
from: ctr from: ctr
path: "/status" path: "/status"
} }
}.contents) @dagger(output) }.contents @dagger(output)
} }
} }

View File

@ -1,7 +1,8 @@
package http package http
import ( import (
"encoding/json" // "encoding/json"
// "strconv"
"alpha.dagger.io/alpine" "alpha.dagger.io/alpine"
"alpha.dagger.io/os" "alpha.dagger.io/os"
@ -12,31 +13,27 @@ TestRequest: {
url: "https://api.github.com/" url: "https://api.github.com/"
request: header: { request: header: {
Accept: "application/json" Accept: "application/json"
Test: ["A", "B"]
} }
} }
testRaw: os.#Container & { testRaw: os.#Container & {
image: alpine.#Image & { image: alpine.#Image & {
package: jq: "~=1.6" package: jq: "~=1.6"
package: bash: true
} }
env: STATUS: "\(req.response.statusCode)" env: STATUS: "\(req.response.statusCode)"
files: "/content.json": { files: "/content.json": {
content: req.response.body content: req.response.body
mode: 0o500 mode: 0o500
} }
command: #""" shell: {
test "$STATUS" = 200 args: ["--noprofile", "--norc", "-eo", "pipefail", "-c"]
test "$(cat /content.json | jq -r .current_user_url)" = "https://api.github.com/user" }
"""# command: #Command
}
testJSON: os.#Container & {
env: STATUS: "\(req.response.statusCode)"
env: CONTENT: json.Unmarshal(req.response.body).current_user_url
command: """
test "$STATUS" = 200
test "$CONTENT" = "https://api.github.com/user"
"""
} }
} }
#Command: #"""
test "$(cat /content.json | jq -r .current_user_url)" = 'https://api.github.com/user'
test "$STATUS" = "200"
"""#