aboutsummaryrefslogtreecommitdiff
path: root/gorankusu_test.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2024-02-13 02:14:35 +0700
committerShulhan <ms@kilabit.info>2024-02-15 01:12:34 +0700
commit372d10a3e1ea01f8d03e44e6ab8be673d05c0773 (patch)
tree02df85cae2d4398cfd74ae193b05f490628cc517 /gorankusu_test.go
parentbc00c07248b1097f6e8f9281edc659cbd4bdd4ff (diff)
downloadgorankusu-372d10a3e1ea01f8d03e44e6ab8be673d05c0773.tar.xz
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
Diffstat (limited to 'gorankusu_test.go')
-rw-r--r--gorankusu_test.go86
1 files changed, 14 insertions, 72 deletions
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)
}