-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dave Brophy
committed
Dec 7, 2017
1 parent
559acf9
commit 5ed9c07
Showing
3 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
package gcsworker | ||
|
||
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, | ||
} | ||
w := New() | ||
w.(*Worker).client = http.DefaultClient | ||
response, err := w.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", | ||
} | ||
w := New() | ||
w.(*Worker).client = http.DefaultClient | ||
response, err := w.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", | ||
}, | ||
} | ||
w := New() | ||
w.(*Worker).client = http.DefaultClient | ||
response, err := w.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, | ||
} | ||
w := New() | ||
w.(*Worker).client = http.DefaultClient | ||
response, err := w.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) | ||
w := New() | ||
w.(*Worker).client = http.DefaultClient | ||
response, err := w.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() | ||
}() | ||
w := New() | ||
w.(*Worker).client = http.DefaultClient | ||
response, err := w.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) | ||
} | ||
} |