diff options
Diffstat (limited to 'http_server_test.go')
| -rw-r--r-- | http_server_test.go | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/http_server_test.go b/http_server_test.go index ab036c6..159c166 100644 --- a/http_server_test.go +++ b/http_server_test.go @@ -1,3 +1,6 @@ +// SPDX-FileCopyrightText: 2023 M. Shulhan <ms@kilabit.info> +// SPDX-License-Identifier: GPL-3.0-or-later + package awwan import ( @@ -12,6 +15,94 @@ import ( "github.com/shuLhan/share/lib/test" ) +func TestHttpServer_Decrypt(t *testing.T) { + type testCase struct { + desc string + expResp string + expErr string + reqBody []byte + } + + var ( + tdata *test.Data + err error + ) + + tdata, err = test.LoadData(`testdata/http_server/decrypt_test.data`) + if err != nil { + t.Fatal(err) + } + + var aww *Awwan + + aww, err = New(`testdata/encrypt-with-passfile`) + if err != nil { + t.Fatal(err) + } + + var httpd *httpServer + + httpd, err = newHttpServer(aww, ``) + if err != nil { + t.Fatal(err) + } + + var endpointDecrypt = &libhttp.Endpoint{ + Method: libhttp.RequestMethodPost, + Path: pathAwwanApiDecrypt, + RequestType: libhttp.RequestTypeJSON, + ResponseType: libhttp.ResponseTypeJSON, + } + + var cases = []testCase{{ + desc: `With valid request`, + reqBody: tdata.Input[`withValidRequest/body`], + expResp: string(tdata.Output[`withValidRequest/Response/body`]), + }, { + desc: `With empty value`, + reqBody: tdata.Input[`withEmptyValue/body`], + expResp: string(tdata.Output[`withEmptyValue/Response/body`]), + }, { + desc: `With directory`, + reqBody: tdata.Input[`withDirectory/body`], + expResp: string(tdata.Output[`withDirectory/Response/body`]), + }, { + desc: `With file not exist`, + reqBody: tdata.Input[`withFileNotExist/body`], + expResp: string(tdata.Output[`withFileNotExist/Response/body`]), + }} + + var ( + httpReq *http.Request + httpRes *http.Response + c testCase + reqBody bytes.Buffer + gotResp bytes.Buffer + resp []byte + ) + + for _, c = range cases { + t.Log(c.desc) + + reqBody.Reset() + reqBody.Write(c.reqBody) + + httpReq = httptest.NewRequest(http.MethodPost, endpointDecrypt.Path, &reqBody) + + var httpWriter = httptest.NewRecorder() + + httpd.ServeHTTP(httpWriter, httpReq) + + httpRes = httpWriter.Result() + + resp, _ = io.ReadAll(httpRes.Body) + + gotResp.Reset() + json.Indent(&gotResp, resp, ``, ` `) + test.Assert(t, c.desc, c.expResp, gotResp.String()) + } +} + func TestHttpServer_Encrypt(t *testing.T) { type testCase struct { desc string |
