Skip to content

Commit

Permalink
httpworker tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dave committed Dec 7, 2017
1 parent 5739ad0 commit 620344b
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 0 deletions.
3 changes: 3 additions & 0 deletions httpworker/httpworker.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,17 @@ func (w *Worker) Send(ctx context.Context, raw map[string]interface{}) (map[stri
ue, ok := err.(*url.Error)
switch {
case response != nil:
// notest
status = response.StatusCode
case ok && ue.Err == context.DeadlineExceeded:
status = "Timeout"
case ok && ue.Err == context.Canceled:
status = "Cancelled"
case ok:
// notest
status = ue.Err.Error()
default:
// notest
status = err.Error()
}
return map[string]interface{}{"status": status}, err
Expand Down
172 changes: 172 additions & 0 deletions httpworker/httpworker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package httpworker

import (
"context"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
"time"
)

func TestSend(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
t.Errorf("Unexpected method: %s", r.Method)
}
}))
defer ts.Close()

payload := map[string]interface{}{
"method": "GET",
"url": ts.URL,
}
response, err := New().Send(context.Background(), payload)
expected := map[string]interface{}{
"status": 200,
}
if err != nil {
log.Fatal(err)
}
if !reflect.DeepEqual(response, expected) {
t.Fatalf("Unexpected: %#v", response)
}
}

func TestPost(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Errorf("Unexpected method: %s", r.Method)
}
b, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Error(err)
}
if string(b) != "abc" {
t.Errorf("Unexpected body: %s", string(b))
}
}))
defer ts.Close()

payload := map[string]interface{}{
"method": "POST",
"url": ts.URL,
"body": "abc",
}
response, err := New().Send(context.Background(), payload)
expected := map[string]interface{}{
"status": 200,
}
if err != nil {
log.Fatal(err)
}
if !reflect.DeepEqual(response, expected) {
t.Fatalf("Unexpected: %#v", response)
}
}

func TestHeaders(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
t.Errorf("Unexpected method: %s", r.Method)
}
if r.Header.Get("a") != "b" || r.Header.Get("c") != "d" {
t.Errorf("Unexpected: a=%s, c=%s", r.Header.Get("a"), r.Header.Get("c"))
}
}))
defer ts.Close()

payload := map[string]interface{}{
"method": "GET",
"url": ts.URL,
"headers": map[string]string{
"a": "b",
"c": "d",
},
}
response, err := New().Send(context.Background(), payload)
expected := map[string]interface{}{
"status": 200,
}
if err != nil {
log.Fatal(err)
}
if !reflect.DeepEqual(response, expected) {
t.Fatalf("Unexpected: %#v", response)
}
}

func TestError(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500 - error"))
}))
defer ts.Close()

payload := map[string]interface{}{
"method": "GET",
"url": ts.URL,
}
response, err := New().Send(context.Background(), payload)
expected := map[string]interface{}{
"status": 500,
}
if err == nil || err.Error() != "non 200 status" {
log.Fatalf("Unexpected error: %v", err)
}
if !reflect.DeepEqual(response, expected) {
t.Fatalf("Unexpected: %#v", response)
}
}

func TestErrorTimeout(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
<-time.After(time.Second)
}))
defer ts.Close()

payload := map[string]interface{}{
"method": "GET",
"url": ts.URL,
}
ctx, _ := context.WithTimeout(context.Background(), time.Millisecond)
response, err := New().Send(ctx, payload)
expected := map[string]interface{}{
"status": "Timeout",
}
if err == nil || !strings.HasSuffix(err.Error(), "context deadline exceeded") {
log.Fatalf("Unexpected error: %v", err)
}
if !reflect.DeepEqual(response, expected) {
t.Fatalf("Unexpected: %#v", response)
}
}

func TestErrorCancel(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
<-time.After(time.Second)
}))
defer ts.Close()

payload := map[string]interface{}{
"method": "GET",
"url": ts.URL,
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
go func() {
cancel()
}()
response, err := New().Send(ctx, payload)
expected := map[string]interface{}{
"status": "Cancelled",
}
if err == nil || !strings.HasSuffix(err.Error(), "context canceled") {
log.Fatalf("Unexpected error: %v", err)
}
if !reflect.DeepEqual(response, expected) {
t.Fatalf("Unexpected: %#v", response)
}
}

0 comments on commit 620344b

Please sign in to comment.