From 372d10a3e1ea01f8d03e44e6ab8be673d05c0773 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Tue, 13 Feb 2024 02:14:35 +0700 Subject: all: move example to root directory The goal is to remove duplicate code in testing and show the example on how to create Gorankusu service from godoc. Implements: https://todo.sr.ht/~shulhan/gorankusu/5 --- gorankusu_test.go | 86 +++++++++---------------------------------------------- 1 file changed, 14 insertions(+), 72 deletions(-) (limited to 'gorankusu_test.go') diff --git a/gorankusu_test.go b/gorankusu_test.go index 99ed214..3ff5f9e 100644 --- a/gorankusu_test.go +++ b/gorankusu_test.go @@ -6,20 +6,17 @@ package gorankusu import ( "crypto/rand" "log" - "net/http" "os" "testing" "time" - libhttp "github.com/shuLhan/share/lib/http" + libnet "github.com/shuLhan/share/lib/net" "github.com/shuLhan/share/lib/test/mock" ) -// dummyHttpd dummy HTTP as target for Gorankusu. -var dummyHttpd *httpdDummy - -// dummyGorankusu the Gorankusu instance that contains [Target] to be tested. -var dummyGorankusu *Gorankusu +// exGorankusu the Gorankusu instance that contains [Target] to be +// tested. +var exGorankusu *Example func TestMain(m *testing.M) { var err error @@ -27,78 +24,23 @@ func TestMain(m *testing.M) { // Mock crypto [rand.Reader] for predictable HTTP boundary. rand.Reader = mock.NewRandReader([]byte(`gorankusu`)) - dummyHttpd, err = newHttpdDummy() + exGorankusu, err = NewExample() if err != nil { log.Fatal(err) } - defer dummyHttpd.Stop(time.Second) - var env = Environment{} + go func() { + var err2 = exGorankusu.Start() + if err2 != nil { + log.Fatal(err2) + } + }() - dummyGorankusu, err = New(&env) + err = libnet.WaitAlive(`tcp`, DefaultListenAddress, 3*time.Second) if err != nil { log.Fatal(err) } - registerTargetHTTP() - - os.Exit(m.Run()) -} - -func registerTargetHTTP() { - var logp = `registerTargetHTTP` - - var target = &Target{ - ID: `target_http`, - Name: `Target HTTP`, - BaseURL: `http://` + dummyHttpd.Server.Options.Address, - } - var targetHTTPUpload = &HTTPTarget{ - ID: `upload`, - Name: `Upload`, - Method: dummyEndpointUpload.Method, - Path: dummyEndpointUpload.Path, - RequestType: dummyEndpointUpload.RequestType, - Params: KeyFormInput{ - `file`: FormInput{ - Label: `File`, - Kind: FormInputKindFile, - }, - }, - RequestDumper: requestDumperWithoutDate, - ResponseDumper: responseDumperWithoutDate, - } - target.HTTPTargets = append(target.HTTPTargets, targetHTTPUpload) - - var targetHTTPRawbodyJSON = &HTTPTarget{ - ID: `rawbody_json`, - Name: `Raw body in JSON`, - Method: dummyEndpointRawbodyJSON.Method, - Path: dummyEndpointRawbodyJSON.Path, - RequestType: dummyEndpointRawbodyJSON.RequestType, - Params: KeyFormInput{ - `ignored`: FormInput{ - Label: `Ignored parameter`, - }, - }, - RequestDumper: requestDumperWithoutDate, - ResponseDumper: responseDumperWithoutDate, - WithRawBody: true, - } - target.HTTPTargets = append(target.HTTPTargets, targetHTTPRawbodyJSON) - - var err = dummyGorankusu.RegisterTarget(target) - if err != nil { - log.Fatalf(`%s: %s`, logp, err) - } -} - -func requestDumperWithoutDate(req *http.Request) ([]byte, error) { - req.Header.Del(libhttp.HeaderDate) - return DumpHTTPRequest(req) -} - -func responseDumperWithoutDate(resp *http.Response) ([]byte, error) { - resp.Header.Del(libhttp.HeaderDate) - return DumpHTTPResponse(resp) + var status = m.Run() + os.Exit(status) } -- cgit v1.3