diff options
| author | Shulhan <ms@kilabit.info> | 2024-02-05 03:21:53 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2024-02-05 03:23:00 +0700 |
| commit | f2cfe0de0eeec8bc7abf9d754b9e89681743ecff (patch) | |
| tree | 802e8a32102a6786b4461e35b916d05b040558d0 /http_server_test.go | |
| parent | f02e4647bae78222196dc06406b5024c1b435bd7 (diff) | |
| download | gorankusu-f2cfe0de0eeec8bc7abf9d754b9e89681743ecff.tar.xz | |
all: implement form input file
The FormInput now can be set to FormInputKindFile that will rendered
as "<input type='file' ...>" on the web user interface.
Once submitted, the file name, type, size, and lastModification will
be stored under FormInput Filename, Filetype, Filesize, and Filemodms.
Implements: https://todo.sr.ht/~shulhan/trunks/1
Diffstat (limited to 'http_server_test.go')
| -rw-r--r-- | http_server_test.go | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/http_server_test.go b/http_server_test.go new file mode 100644 index 0000000..a1e75b5 --- /dev/null +++ b/http_server_test.go @@ -0,0 +1,105 @@ +// SPDX-FileCopyrightText: 2024 M. Shulhan <ms@kilabit.info> +// SPDX-License-Identifier: GPL-3.0-or-later + +package trunks + +import ( + "bytes" + "encoding/json" + "io" + "net/http/httptest" + "testing" + + libhttp "github.com/shuLhan/share/lib/http" + "github.com/shuLhan/share/lib/test" +) + +type httpRequestParams struct { + method string + path string + body []byte +} + +func TestTrunksAPITargetRunHTTP_formInputFile(t *testing.T) { + type testCase struct { + tag string + } + + var ( + tdata *test.Data + err error + ) + tdata, err = test.LoadData(`testdata/target_http_run_formkindfile_test.txt`) + if err != nil { + t.Fatal(err) + } + + var listCase = []testCase{{ + tag: `valid`, + }} + + var ( + c testCase + runResp RunResponse + tag string + exp string + ) + + for _, c = range listCase { + var reqparams = httpRequestParams{ + method: apiTargetRunHTTP.Method.String(), + path: apiTargetRunHTTP.Path, + body: tdata.Input[c.tag+`:http_request_body`], + } + + _, runResp = dummyTrunksServe(t, reqparams) + + tag = c.tag + `:RunResponse.DumpResponse` + exp = string(tdata.Output[tag]) + test.Assert(t, tag, exp, string(runResp.DumpResponse)) + } +} + +func dummyTrunksServe(t *testing.T, reqparams httpRequestParams) (rawResp []byte, runResp RunResponse) { + var body bytes.Buffer + var recorder = httptest.NewRecorder() + + body.Write(reqparams.body) + + var httpreq = httptest.NewRequest( + reqparams.method, + reqparams.path, + &body) + + dummyTrunks.Httpd.ServeHTTP(recorder, httpreq) + + var ( + httpres = recorder.Result() + got []byte + err error + ) + + got, err = io.ReadAll(httpres.Body) + if err != nil { + t.Fatal(err) + } + + body.Reset() + err = json.Indent(&body, got, ``, ` `) + if err != nil { + t.Fatal(err) + } + + rawResp = body.Bytes() + + var epres = libhttp.EndpointResponse{ + Data: &runResp, + } + + err = json.Unmarshal(rawResp, &epres) + if err != nil { + t.Fatal(err) + } + + return rawResp, runResp +} |
