Skip to content

Commit

Permalink
Standardise protocol keys according to https://pkg.go.dev/net/http#Re…
Browse files Browse the repository at this point in the history
  • Loading branch information
georgemalamidis-lh authored Mar 15, 2022
1 parent 49bc8b2 commit f71680f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ An example ripley request:
```JSON
{
"url": "http://localhost:8080/",
"verb": "POST",
"method": "POST",
"body": "{\"foo\": \"bar\"}",
"headers": {
"Accept": "text/plain"
Expand All @@ -58,14 +58,14 @@ An example ripley request:
}
```

`url`, `verb` and `timestamp` are required, `headers` and `body` are optional.
`url`, `method` and `timestamp` are required, `headers` and `body` are optional.

`-pace` specifies rate phases in `[duration]@[rate]` format. For example, `10s@5 5m@10 1h30m@100` means replay traffic at 5x for 10 seconds, 10x for 5 minutes and 100x for one and a half hours. The run will stop either when ripley stops receiving requests from `STDIN` or when the last phase elapses, whichever happens first.

Ripley writes request results as JSON Lines to `STDOUT`

```bash
echo '{"url": "http://localhost:8080/", "verb": "GET", "timestamp": "2021-11-08T18:59:50.9Z"}' | ./ripley | jq
echo '{"url": "http://localhost:8080/", "method": "GET", "timestamp": "2021-11-08T18:59:50.9Z"}' | ./ripley | jq
```

produces
Expand All @@ -75,7 +75,7 @@ produces
"statusCode": 200,
"latency": 3915447,
"request": {
"verb": "GET",
"method": "GET",
"url": "http://localhost:8080/",
"body": "",
"timestamp": "2021-11-08T18:59:50.9Z",
Expand Down
20 changes: 10 additions & 10 deletions etc/requests.jsonl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{"url": "http://localhost:8080/", "verb": "GET", "timestamp": "2021-11-08T18:59:50.9Z"}
{"url": "http://localhost:8080/", "verb": "GET", "timestamp": "2021-11-08T18:59:51.9Z"}
{"url": "http://localhost:8080/", "verb": "GET", "timestamp": "2021-11-08T18:59:52.9Z"}
{"url": "http://localhost:8080/", "verb": "GET", "timestamp": "2021-11-08T18:59:53.9Z"}
{"url": "http://localhost:8080/", "verb": "GET", "timestamp": "2021-11-08T18:59:54.9Z"}
{"url": "http://localhost:8080/", "verb": "GET", "timestamp": "2021-11-08T18:59:55.9Z"}
{"url": "http://localhost:8080/", "verb": "GET", "timestamp": "2021-11-08T18:59:56.9Z"}
{"url": "http://localhost:8080/", "verb": "GET", "timestamp": "2021-11-08T18:59:57.9Z"}
{"url": "http://localhost:8080/", "verb": "POST", "body": "{\"foo\": \"bar\"}", "headers": {"Accept": "text/plain"}, "timestamp": "2021-11-08T18:59:58.9Z"}
{"url": "http://localhost:8080/", "verb": "GET", "headers": {"Accept": "text/plain"}, "timestamp": "2021-11-08T18:59:59.9Z"}
{"url": "http://localhost:8080/", "method": "GET", "timestamp": "2021-11-08T18:59:50.9Z"}
{"url": "http://localhost:8080/", "method": "GET", "timestamp": "2021-11-08T18:59:51.9Z"}
{"url": "http://localhost:8080/", "method": "GET", "timestamp": "2021-11-08T18:59:52.9Z"}
{"url": "http://localhost:8080/", "method": "GET", "timestamp": "2021-11-08T18:59:53.9Z"}
{"url": "http://localhost:8080/", "method": "GET", "timestamp": "2021-11-08T18:59:54.9Z"}
{"url": "http://localhost:8080/", "method": "GET", "timestamp": "2021-11-08T18:59:55.9Z"}
{"url": "http://localhost:8080/", "method": "GET", "timestamp": "2021-11-08T18:59:56.9Z"}
{"url": "http://localhost:8080/", "method": "GET", "timestamp": "2021-11-08T18:59:57.9Z"}
{"url": "http://localhost:8080/", "method": "POST", "body": "{\"foo\": \"bar\"}", "headers": {"Accept": "text/plain"}, "timestamp": "2021-11-08T18:59:58.9Z"}
{"url": "http://localhost:8080/", "method": "GET", "headers": {"Accept": "text/plain"}, "timestamp": "2021-11-08T18:59:59.9Z"}
16 changes: 8 additions & 8 deletions pkg/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ import (
)

var (
validVerbs = [9]string{"GET", "HEAD", "POST", "PUT", "DELETE", "CONNECT", "OPTIONS", "TRACE", "PATCH"}
validMethods = [9]string{"GET", "HEAD", "POST", "PUT", "DELETE", "CONNECT", "OPTIONS", "TRACE", "PATCH"}
)

type request struct {
Verb string `json:"verb"`
Method string `json:"method"`
Url string `json:"url"`
Body string `json:"body"`
Timestamp time.Time `json:"timestamp"`
Headers map[string]string `json:"headers"`
}

func (r *request) httpRequest() (*http.Request, error) {
req, err := http.NewRequest(r.Verb, r.Url, bytes.NewReader([]byte(r.Body)))
req, err := http.NewRequest(r.Method, r.Url, bytes.NewReader([]byte(r.Body)))

if err != nil {
return nil, err
Expand All @@ -67,8 +67,8 @@ func unmarshalRequest(jsonRequest []byte) (*request, error) {

// Validate

if !validVerb(req.Verb) {
return nil, errors.New(fmt.Sprintf("Invalid verb: %s", req.Verb))
if !validMethod(req.Method) {
return nil, errors.New(fmt.Sprintf("Invalid method: %s", req.Method))
}

if req.Url == "" {
Expand All @@ -82,9 +82,9 @@ func unmarshalRequest(jsonRequest []byte) (*request, error) {
return req, nil
}

func validVerb(requestVerb string) bool {
for _, verb := range validVerbs {
if requestVerb == verb {
func validMethod(requestMethod string) bool {
for _, method := range validMethods {
if requestMethod == method {
return true
}
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,29 @@ import (
"time"
)

func TestUnrmarshalInvalidVerb(t *testing.T) {
jsonRequest := `{"verb": "WHAT"}`
func TestUnrmarshalInvalidMethod(t *testing.T) {
jsonRequest := `{"method": "WHAT"}`
req, err := unmarshalRequest([]byte(jsonRequest))

if req != nil {
t.Errorf("req = %v; want nil", req)
}

if err.Error() != "Invalid verb: WHAT" {
t.Errorf(`err.Error() = %v; want "Invalid verb: WHAT"`, err.Error())
if err.Error() != "Invalid method: WHAT" {
t.Errorf(`err.Error() = %v; want "Invalid method: WHAT"`, err.Error())
}
}

func TestUnrmarshalValid(t *testing.T) {
jsonRequest := `{"verb": "GET", "url": "http://example.com", "timestamp": "2021-11-08T18:59:59.9Z"}`
jsonRequest := `{"method": "GET", "url": "http://example.com", "timestamp": "2021-11-08T18:59:59.9Z"}`
req, err := unmarshalRequest([]byte(jsonRequest))

if err != nil {
t.Errorf("err = %v; want nil", err)
}

if req.Verb != "GET" {
t.Errorf("req.Verb = %v; want GET", req.Verb)
if req.Method != "GET" {
t.Errorf("req.Method = %v; want GET", req.Method)
}

if req.Url != "http://example.com" {
Expand Down

0 comments on commit f71680f

Please sign in to comment.