Skip to content

Commit 9e7da09

Browse files
author
frostwind
committed
Added raw body to request
1 parent 4179ac9 commit 9e7da09

File tree

7 files changed

+58
-22
lines changed

7 files changed

+58
-22
lines changed

assets/javascripts/controllers/new_test.coffee

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ module.exports = class
2525
method: 'GET'
2626
path: ''
2727
headers: []
28+
rawBody: ''
2829

2930
removeTask: (task) =>
3031
@scope.test.tasks = _(@scope.test.tasks).reject (t) ->

assets/views/new_test.html

+5
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ <h2>How to?</h2>
6565
</li>
6666
</ul>
6767
</fieldset>
68+
<fieldset>
69+
<h2></h2>
70+
<label for="">Raw body</label>
71+
<input type="text" placeholder="" ng-model="task.rawBody">
72+
</fieldset>
6873
<fieldset>
6974
<h2>Headers</h2>
7075
<button ng-click="addHeader(task)">Add header</button>

gopherling.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func startTest(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
130130
l := loader.New(t.Requests, t.Concurrency, conn)
131131

132132
for _, task := range t.Tasks {
133-
l.AddTasks(task.Method, t.BaseUrl, task.Path, task.Headers)
133+
l.AddTasks(task.Method, t.BaseUrl, task.Path, task.Headers, task.RawBody)
134134
}
135135

136136
l.Run()

loader/loader.go

+43-20
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package loader
22

33
import (
4+
"bytes"
45
"fmt"
6+
"github.com/gophergala/gopherling/models"
57
"github.com/gorilla/websocket"
68
"net/http"
79
"sync"
810
"time"
9-
"github.com/gophergala/gopherling/models"
1011
)
1112

1213
type feedback struct {
@@ -15,40 +16,62 @@ type feedback struct {
1516
Duration time.Duration `json:"duration"`
1617
}
1718

18-
type loader struct {
19-
n int
20-
c int
21-
22-
requests []*http.Request
23-
ws *websocket.Conn
19+
type Task struct {
20+
method string
21+
host string
22+
path string
23+
headers []models.Header
24+
rawBody string
2425
}
2526

26-
func New(n, c int, ws *websocket.Conn) *loader {
27-
requests := make([]*http.Request, 0)
28-
return &loader{n, c, requests, ws}
29-
}
27+
func (t *Task) Request() *http.Request {
28+
body := bytes.NewBufferString(t.rawBody)
3029

31-
func (l *loader) AddTasks(method string, host string, path string, headers []models.Header) {
32-
req, err := http.NewRequest(method, host+"/"+path, nil)
30+
req, err := http.NewRequest(t.method, t.host+"/"+t.path, body)
3331

34-
for _, header := range headers {
32+
for _, header := range t.headers {
3533
req.Header.Set(header.Field, header.Value)
3634
}
3735

3836
if err != nil {
3937
fmt.Println("something went wrong !")
4038
}
4139

42-
l.requests = append(l.requests, req)
40+
return req
41+
}
42+
43+
type loader struct {
44+
n int
45+
c int
46+
47+
tasks []*Task
48+
ws *websocket.Conn
49+
}
50+
51+
func New(n, c int, ws *websocket.Conn) *loader {
52+
tasks := make([]*Task, 0)
53+
return &loader{n, c, tasks, ws}
54+
}
55+
56+
func (l *loader) AddTasks(method string, host string, path string, headers []models.Header, rawBody string) {
57+
t := &Task{method: method,
58+
host: host,
59+
path: path,
60+
headers: headers,
61+
rawBody: rawBody,
62+
}
63+
64+
l.tasks = append(l.tasks, t)
4365
}
4466

45-
func (l *loader) spawnClient(wg *sync.WaitGroup, queue chan []*http.Request) {
67+
func (l *loader) spawnClient(wg *sync.WaitGroup, queue chan []*Task) {
4668
// this is one out of c clients
4769
client := &http.Client{}
4870

4971
// if we're not busy, get a tasks list from the queue channel
50-
for requests := range queue {
51-
for i, req := range requests {
72+
for tasks := range queue {
73+
for i, task := range tasks {
74+
req := task.Request()
5275
start := time.Now()
5376
// Send the request
5477
res, err := client.Do(req)
@@ -75,7 +98,7 @@ func (l *loader) Run() {
7598
wg.Add(l.n)
7699

77100
// The queue channel will contain an array of tasks (up to n time)
78-
queue := make(chan []*http.Request, l.n)
101+
queue := make(chan []*Task, l.n)
79102

80103
// Spawn all our clients
81104
for i := 0; i < l.c; i++ {
@@ -86,7 +109,7 @@ func (l *loader) Run() {
86109

87110
// Populate our channel with tasks arrays
88111
for i := 0; i < l.n; i++ {
89-
queue <- l.requests
112+
queue <- l.tasks
90113
}
91114

92115
// Close our channel

models/models.go

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type Task struct {
1313
Method string `bson:"method" json:"method"`
1414
Path string `bson:"path" json:"path"`
1515
Headers []Header `bson:"headers" json:"headers"`
16+
RawBody string `bson:"raw_body" json:"rawBody"`
1617
}
1718

1819
type Test struct {

static/javascripts/app.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ module.exports = (function() {
113113
return this.scope.test.tasks.push({
114114
method: 'GET',
115115
path: '',
116-
headers: []
116+
headers: [],
117+
rawBody: ''
117118
});
118119
};
119120

static/views/new_test.html

+5
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ <h2>How to?</h2>
6565
</li>
6666
</ul>
6767
</fieldset>
68+
<fieldset>
69+
<h2></h2>
70+
<label for="">Raw body</label>
71+
<input type="text" placeholder="" ng-model="task.rawBody">
72+
</fieldset>
6873
<fieldset>
6974
<h2>Headers</h2>
7075
<button ng-click="addHeader(task)">Add header</button>

0 commit comments

Comments
 (0)