aboutsummaryrefslogtreecommitdiff
path: root/target.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2021-03-12 02:11:11 +0700
committerShulhan <ms@kilabit.info>2021-03-15 01:52:45 +0700
commitd30bfb7fa1c32984eaba8ad1cf744328812e02b9 (patch)
tree316e089ac24694a3793ff92475be531ccd444b74 /target.go
downloadgorankusu-d30bfb7fa1c32984eaba8ad1cf744328812e02b9.tar.xz
trunks: a module for testing HTTP services
Trunks is a library and HTTP service that provide web user interface to test HTTP service, similar to Postman, and for load testing. For the load testing we use vegeta [1] as the backend. [1] https://github.com/tsenart/vegeta
Diffstat (limited to 'target.go')
-rw-r--r--target.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/target.go b/target.go
new file mode 100644
index 0000000..b01eb52
--- /dev/null
+++ b/target.go
@@ -0,0 +1,62 @@
+// Copyright 2021, Shulhan <ms@kilabit.info>. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package trunks
+
+import (
+ "fmt"
+
+ libhttp "github.com/shuLhan/share/lib/http"
+)
+
+//
+// Target contains group of HTTP and/or WebSocket endpoints that can be tested
+// by Trunks.
+//
+type Target struct {
+ ID string
+ Name string
+ AttackOpts *AttackOptions
+ Vars map[string]string
+ HttpTargets []*HttpTarget
+
+ // HttpClient that can be used for running HttpTarget.
+ HttpClient *libhttp.Client `json:"-"`
+}
+
+func (target *Target) init() (err error) {
+ if len(target.Name) == 0 {
+ return fmt.Errorf("Target.Name is empty")
+ }
+
+ target.ID = generateID(target.Name)
+
+ if target.AttackOpts == nil {
+ target.AttackOpts = &AttackOptions{}
+ }
+
+ err = target.AttackOpts.init()
+ if err != nil {
+ return err
+ }
+
+ if target.Vars == nil {
+ target.Vars = make(map[string]string, 0)
+ }
+
+ for _, ht := range target.HttpTargets {
+ ht.init()
+ }
+
+ return nil
+}
+
+func (target *Target) getHttpTargetByID(id string) *HttpTarget {
+ for _, ht := range target.HttpTargets {
+ if ht.ID == id {
+ return ht
+ }
+ }
+ return nil
+}