Files
hey/requester/requester_test.go
Jaana Burcu Dogan 90614616f3 all: rename the tool to hey, add minor fixes
DO NOT MERGE

It was a mistake that this tool has been published with the same
name as tarekziade's boom. To avoid the conflict, we should rename the
tool at the cost of breaking the existing users.

Fixes #130.
2016-09-02 02:46:30 -07:00

136 lines
3.2 KiB
Go

// Copyright 2014 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package requester
import (
"bytes"
"io/ioutil"
"net/http"
"net/http/httptest"
"sync"
"sync/atomic"
"testing"
"time"
)
func TestN(t *testing.T) {
var count int64
handler := func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt64(&count, int64(1))
}
server := httptest.NewServer(http.HandlerFunc(handler))
defer server.Close()
req, _ := http.NewRequest("GET", server.URL, nil)
w := &Work{
Request: req,
N: 20,
C: 2,
}
w.Run()
if count != 20 {
t.Errorf("Expected to boom 20 times, found %v", count)
}
}
func TestQps(t *testing.T) {
var wg sync.WaitGroup
var count int64
handler := func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt64(&count, int64(1))
}
server := httptest.NewServer(http.HandlerFunc(handler))
defer server.Close()
req, _ := http.NewRequest("GET", server.URL, nil)
w := &Work{
Request: req,
N: 20,
C: 2,
Qps: 1,
}
wg.Add(1)
time.AfterFunc(time.Second, func() {
if count > 1 {
t.Errorf("Expected to work 1 times, found %v", count)
}
wg.Done()
})
go w.Run()
wg.Wait()
}
func TestRequest(t *testing.T) {
var uri, contentType, some, method, auth string
handler := func(w http.ResponseWriter, r *http.Request) {
uri = r.RequestURI
method = r.Method
contentType = r.Header.Get("Content-type")
some = r.Header.Get("X-some")
auth = r.Header.Get("Authorization")
}
server := httptest.NewServer(http.HandlerFunc(handler))
defer server.Close()
header := make(http.Header)
header.Add("Content-type", "text/html")
header.Add("X-some", "value")
req, _ := http.NewRequest("GET", server.URL, nil)
req.Header = header
req.SetBasicAuth("username", "password")
w := &Work{
Request: req,
N: 1,
C: 1,
}
w.Run()
if uri != "/" {
t.Errorf("Uri is expected to be /, %v is found", uri)
}
if contentType != "text/html" {
t.Errorf("Content type is expected to be text/html, %v is found", contentType)
}
if some != "value" {
t.Errorf("X-some header is expected to be value, %v is found", some)
}
if auth != "Basic dXNlcm5hbWU6cGFzc3dvcmQ=" {
t.Errorf("Basic authorization is not properly set")
}
}
func TestBody(t *testing.T) {
var count int64
handler := func(w http.ResponseWriter, r *http.Request) {
body, _ := ioutil.ReadAll(r.Body)
if string(body) == "Body" {
atomic.AddInt64(&count, 1)
}
}
server := httptest.NewServer(http.HandlerFunc(handler))
defer server.Close()
req, _ := http.NewRequest("POST", server.URL, bytes.NewBuffer([]byte("Body")))
w := &Work{
Request: req,
RequestBody: "Body",
N: 10,
C: 1,
}
w.Run()
if count != 10 {
t.Errorf("Expected to work 10 times, found %v", count)
}
}