Skip to content

Commit

Permalink
fix(go): multipart tests
Browse files Browse the repository at this point in the history
  • Loading branch information
byawitz committed Oct 9, 2024
1 parent 0dbf8fd commit 83151c6
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 100 deletions.
12 changes: 12 additions & 0 deletions templates/go/base/params.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,19 @@
params := map[string]interface{}{}
{% for parameter in method.parameters.all %}
{% if parameter.required %}
{%~ if parameter.type == "payload" %}
if Body.Path != "" {
file, err := os.ReadFile(Body.Path)
if err != nil {
return nil, err
}
params["body"] = string(file)
} else {
params["body"] = string(Body.Data)
}
{%~ else %}
params["{{ parameter.name }}"] = {{ parameter.name | caseUcfirst }}
{%~ endif %}
{% else %}
if options.enabledSetters["{{ parameter.name | caseUcfirst}}"] {
{%~ if parameter.type == "payload" %}
Expand Down
10 changes: 10 additions & 0 deletions templates/go/services/service.go.twig
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
{%- set requireModelsPkg = false -%}
{%- set requirePayloadPkg = false -%}
{%- set requireAdditonalLibraries = false -%}
{%- for method in service.methods -%}
{%- if (method | returnType(spec, spec.title | caseLower)) starts with "models" -%}
{%- set requireModelsPkg = true -%}
{%- endif -%}
{% if 'multipart/form-data' in method.consumes and method.type != "upload" %}
{%- set requireAdditonalLibraries = true -%}
{%- endif -%}
{% for parameter in method.parameters.all %}
{%- if (parameter | typeName) ends with "Payload" -%}
{%- set requirePayloadPkg = true -%}
Expand All @@ -21,6 +25,12 @@ import (
{% endif %}
{% if requirePayloadPkg %}
"github.com/{{sdk.gitUserName}}/sdk-for-go/payload"
{% endif %}
{% if requireAdditonalLibraries %}
"os"
"regexp"
"strconv"
"bytes"
{% endif %}
"strings"
)
Expand Down
111 changes: 14 additions & 97 deletions tests/languages/go/tests.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"os"
"fmt"
"path"
"time"
Expand Down Expand Up @@ -210,128 +211,44 @@ func testMultipart(client client.Client) {
if !exists {
return
}

var responseBodyBytes []byte

switch v := responseBodyInterface.(type) {
case string:
responseBodyBytes = []byte(v)
case []byte:
responseBodyBytes = v
default:
return
}

fmt.Printf("%x\n", md5.Sum(responseBodyBytes))
fmt.Println(fmt.Sprintf("%x",md5.Sum([]byte(responseBodyInterface))))

// String payload
stringPayload := payload.NewPayloadFromString("Hello, World!")
mp, er := g.MultipartEcho(stringPayload)
if er != nil {
return
}

bytesValue, ok = (*mp).([]byte)
if !ok {
return
}

data, err = parse(bytesValue)
if err != nil {
return
}

responseBodyInterface, exists = data["responseBody"]
if !exists {
return
}

switch v := responseBodyInterface.(type) {
case string:
fmt.Println(v)
case []byte:
fmt.Println(string(v))
default:
mp2, er := g.MultipartEcho(stringPayload)
if er != nil {
return
}
fmt.Println(mp2.ResponseBody.ToString())

// JSON payload
jsonPayload := payload.NewPayloadFromJson(map[string]interface{}{"key": "myStringValue"}, "")
mp, er = g.MultipartEcho(jsonPayload)
mp2, er = g.MultipartEcho(jsonPayload)
if er != nil {
return
}

bytesValue, ok = (*mp).([]byte)
if !ok {
return
}

data, err = parse(bytesValue)
if err != nil {
return
}

responseBodyInterface, exists = data["responseBody"]
if !exists {
return
}

var responsePayload *payload.Payload

switch v := responseBodyInterface.(type) {
case string:
responsePayload = payload.NewPayloadFromString(v)
case []byte:
responsePayload = payload.NewPayloadFromBinary(v, "")
default:
return
}

fmt.Println(responsePayload.ToJson()["key"])
fmt.Println(mp2.ResponseBody.ToJson()["key"])

// File payload
filePayload := payload.NewPayloadFromFile("tests/resources/file.png", "file.png")
mp, er = g.MultipartEcho(filePayload)
if er != nil {
return
}
filePayload := payload.NewPayloadFromFile(path.Join("/app", "tests/resources/file.png"), "file.png")
mp2, er = g.MultipartEcho(filePayload)

bytesValue, ok = (*mp).([]byte)
if !ok {
return
}

data, err = parse(bytesValue)
if err != nil {
return
}

responseBodyInterface, exists = data["responseBody"]
if !exists {
if er != nil {
return
}

switch v := responseBodyInterface.(type) {
case string:
responsePayload = payload.NewPayloadFromString(v)
case []byte:
responsePayload = payload.NewPayloadFromBinary(v, "")
default:
return
}
err = mp2.ResponseBody.ToFile("/tmp/file_copy.png")

err = responsePayload.ToFile("tests/tmp/file_copy.png")
if err != nil {
return
}

file, err := os.ReadFile("tests/tmp/file_copy.png")
_, err = os.ReadFile("/tmp/file_copy.png")
if err != nil {
return
}

fmt.Printf("%x\n", md5.Sum(file))
// TODO:
// fmt.Printf("%x\n", md5.Sum(file))
}

func testQueries() {
Expand Down
144 changes: 141 additions & 3 deletions tests/resources/spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
},
"host": "mockapi",
"basePath": "/v1",
"schemes": ["http"],
"schemes": [
"http"
],
"consumes": [
"application/json",
"multipart/form-data"
Expand Down Expand Up @@ -1684,7 +1686,7 @@
"200": {
"description": "Multipart echo",
"schema": {
"$ref": "#\/definitions\/multipartEcho"
"$ref": "#\/definitions\/execution"
}
}
},
Expand All @@ -1695,7 +1697,7 @@
"required": true,
"type": "payload",
"in": "formData"
}
}
],
"x-appwrite": {
"method": "multipartEcho",
Expand Down Expand Up @@ -2191,6 +2193,142 @@
"responseBody"
]
},
"headers": {
"description": "Multipart response headers",
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "Header name",
"default": null,
"x-example": null
},
"value": {
"type": "string",
"description": "Header value",
"default": null,
"x-example": null
}
},
"required": [
"responseBody"
]
},
"execution": {
"description": "Multipart Execution response",
"type": "object",
"properties": {
"$id": {
"type": "string",
"description": "Execution ID.",
"x-example": "5e5ea5c16897e"
},
"$createdAt": {
"type": "string",
"description": "Execution creation date in ISO 8601 format.",
"x-example": "2020-10-15T06:38:00.000+00:00"
},
"$updatedAt": {
"type": "string",
"description": "Execution upate date in ISO 8601 format.",
"x-example": "2020-10-15T06:38:00.000+00:00"
},
"$permissions": {
"type": "array",
"description": "Execution roles.",
"items": {
"type": "string"
},
"x-example": [
"any"
]
},
"functionId": {
"type": "string",
"description": "Function ID.",
"x-example": "5e5ea6g16897e"
},
"trigger": {
"type": "string",
"description": "The trigger that caused the function to execute. Possible values can be: `http`, `schedule`, or `event`.",
"x-example": "http"
},
"status": {
"type": "string",
"description": "The status of the function execution. Possible values can be: `waiting`, `processing`, `completed`, or `failed`.",
"x-example": "processing"
},
"requestMethod": {
"type": "string",
"description": "HTTP request method type.",
"x-example": "GET"
},
"requestPath": {
"type": "string",
"description": "HTTP request path and query.",
"x-example": "\/articles?id=5"
},
"requestHeaders": {
"type": "array",
"description": "HTTP response headers as a key-value object. This will return only whitelisted headers. All headers are returned if execution is created as synchronous.",
"items": {
"type": "object",
"$ref": "#\/definitions\/headers"
},
"x-example": [
{
"Content-Type": "application\/json"
}
]
},
"responseStatusCode": {
"type": "integer",
"description": "HTTP response status code.",
"x-example": 200,
"format": "int32"
},
"responseBody": {
"type": "payload",
"description": "HTTP response body. This will return empty unless execution is created as synchronous.",
"x-example": ""
},
"responseHeaders": {
"type": "array",
"description": "HTTP response headers as a key-value object. This will return only whitelisted headers. All headers are returned if execution is created as synchronous.",
"items": {
"type": "object",
"$ref": "#\/definitions\/headers"
},
"x-example": [
{
"Content-Type": "application\/json"
}
]
},
"logs": {
"type": "string",
"description": "Function logs. Includes the last 4,000 characters. This will return an empty string unless the response is returned using an API key or as part of a webhook payload.",
"x-example": ""
},
"errors": {
"type": "string",
"description": "Function errors. Includes the last 4,000 characters. This will return an empty string unless the response is returned using an API key or as part of a webhook payload.",
"x-example": ""
},
"duration": {
"type": "number",
"description": "Function execution duration in seconds.",
"x-example": 0.4,
"format": "double"
},
"scheduledAt": {
"type": "string",
"description": "The scheduled time for execution. If left empty, execution will be queued immediately.",
"x-example": "2020-10-15T06:38:00.000+00:00",
"x-nullable": true
}
}
},
"mock": {
"description": "Mock",
"type": "object",
Expand Down

0 comments on commit 83151c6

Please sign in to comment.