aboutsummaryrefslogtreecommitdiff
path: root/example/example.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2021-03-27 19:08:41 +0700
committerShulhan <ms@kilabit.info>2021-03-27 19:08:41 +0700
commita098c7d1641299e14ddfa88800922c128feff2d0 (patch)
treebf0729b0de68654db1b4d64ec7cb47454d03b4f4 /example/example.go
parent413ed58f7df58644f030ddf70cbd3b234270e579 (diff)
downloadgorankusu-a098c7d1641299e14ddfa88800922c128feff2d0.tar.xz
all: allow client to customize request method, path, and type
Previously, the HttpTarget Method, Path, and RequestType is fixed. Once its declared, the Run method will use the defined values to generate and call HTTP request. This changes add IsCustomizable field on HttpTarget. If its true, client or web user interface can modify the request method, path, and type. The values send by client to server after changes will be used instead of the fixed values. This changes also make the Run handler on HttpTarget to be optional. If its not defined, it will generated the HTTP request from the method, path, and type passed to server.
Diffstat (limited to 'example/example.go')
-rw-r--r--example/example.go31
1 files changed, 21 insertions, 10 deletions
diff --git a/example/example.go b/example/example.go
index 1d6de3e..ba2dada 100644
--- a/example/example.go
+++ b/example/example.go
@@ -21,8 +21,7 @@ import (
)
const (
- pathExampleGet = "/example/get"
- pathExamplePostForm = "/example/post/form"
+ pathExample = "/example"
)
const (
@@ -101,7 +100,7 @@ func (ex *Example) Stop() {
func (ex *Example) registerEndpoints() (err error) {
err = ex.trunks.Server.RegisterEndpoint(&libhttp.Endpoint{
Method: libhttp.RequestMethodGet,
- Path: pathExampleGet,
+ Path: pathExample,
RequestType: libhttp.RequestTypeQuery,
ResponseType: libhttp.ResponseTypeJSON,
Call: ex.pathExampleGet,
@@ -112,7 +111,7 @@ func (ex *Example) registerEndpoints() (err error) {
err = ex.trunks.Server.RegisterEndpoint(&libhttp.Endpoint{
Method: libhttp.RequestMethodPost,
- Path: pathExamplePostForm,
+ Path: pathExample,
RequestType: libhttp.RequestTypeForm,
ResponseType: libhttp.ResponseTypeJSON,
Call: ex.pathExamplePostForm,
@@ -122,7 +121,7 @@ func (ex *Example) registerEndpoints() (err error) {
}
func (ex *Example) registerWebSocketEndpoints() (err error) {
- err = ex.wsServer.RegisterTextHandler(http.MethodGet, pathExampleGet,
+ err = ex.wsServer.RegisterTextHandler(http.MethodGet, pathExample,
ex.handleWSExampleGet)
if err != nil {
return err
@@ -144,7 +143,7 @@ func (ex *Example) registerTargets() (err error) {
HttpTargets: []*trunks.HttpTarget{{
Name: "HTTP Get",
Method: libhttp.RequestMethodGet,
- Path: pathExampleGet,
+ Path: pathExample,
RequestType: libhttp.RequestTypeQuery,
Headers: trunks.KeyValue{
"X-Get": "1",
@@ -159,7 +158,7 @@ func (ex *Example) registerTargets() (err error) {
}, {
Name: "HTTP Post Form",
Method: libhttp.RequestMethodPost,
- Path: pathExamplePostForm,
+ Path: pathExample,
RequestType: libhttp.RequestTypeForm,
Headers: trunks.KeyValue{
"X-PostForm": "1",
@@ -172,6 +171,18 @@ func (ex *Example) registerTargets() (err error) {
AllowAttack: true,
PreAttack: ex.preattackExamplePostForm,
Attack: ex.attackExamplePostForm,
+ }, {
+ Name: "HTTP free form",
+ Method: libhttp.RequestMethodGet,
+ Path: pathExample,
+ RequestType: libhttp.RequestTypeForm,
+ Headers: trunks.KeyValue{
+ "X-FreeForm": "1",
+ },
+ Params: trunks.KeyValue{
+ "Param1": "123",
+ },
+ IsCustomizable: true,
}},
}
@@ -207,7 +218,7 @@ func (ex *Example) registerTargets() (err error) {
func (ex *Example) pathExampleGet(epr *libhttp.EndpointRequest) ([]byte, error) {
res := libhttp.EndpointResponse{}
res.Code = http.StatusOK
- res.Message = pathExampleGet
+ res.Message = pathExample
res.Data = epr.HttpRequest.Form
return json.Marshal(&res)
@@ -216,7 +227,7 @@ func (ex *Example) pathExampleGet(epr *libhttp.EndpointRequest) ([]byte, error)
func (ex *Example) pathExamplePostForm(epr *libhttp.EndpointRequest) ([]byte, error) {
res := libhttp.EndpointResponse{}
res.Code = http.StatusOK
- res.Message = pathExamplePostForm
+ res.Message = pathExample
res.Data = epr.HttpRequest.Form
return json.Marshal(&res)
@@ -330,7 +341,7 @@ func (ex *Example) runWebSocketGet(rr *trunks.RunRequest) (resbody []byte, err e
req := websocket.Request{
ID: uint64(time.Now().UnixNano()),
Method: http.MethodGet,
- Target: pathExampleGet,
+ Target: pathExample,
Body: string(body),
}