aboutsummaryrefslogtreecommitdiff
path: root/response_test.go
diff options
context:
space:
mode:
authorAdnan Maolood <[email protected]>2021-02-15 19:20:37 -0500
committerAdnan Maolood <[email protected]>2021-02-15 19:20:37 -0500
commitec269c5c9d8bb338e472716f7c15b09235cade4b (patch)
treec1b1dea3ea639e4e2a6d09fbfd92c2affcbb9f95 /response_test.go
parentReturn ErrInvalidResponse on error reading status (diff)
downloadgo-gemini-ec269c5c9d8bb338e472716f7c15b09235cade4b.tar.xz
go-gemini-ec269c5c9d8bb338e472716f7c15b09235cade4b.zip
Add some tests
Diffstat (limited to 'response_test.go')
-rw-r--r--response_test.go104
1 files changed, 104 insertions, 0 deletions
diff --git a/response_test.go b/response_test.go
new file mode 100644
index 0000000..abf71c0
--- /dev/null
+++ b/response_test.go
@@ -0,0 +1,104 @@
+package gemini
+
+import (
+ "io"
+ "io/ioutil"
+ "strings"
+ "testing"
+)
+
+func TestReadResponse(t *testing.T) {
+ tests := []struct {
+ Raw string
+ Status int
+ Meta string
+ Body string
+ Err error
+ }{
+ {
+ Raw: "20 text/gemini\r\nHello, world!\nWelcome to my capsule.",
+ Status: 20,
+ Meta: "text/gemini",
+ Body: "Hello, world!\nWelcome to my capsule.",
+ },
+ {
+ Raw: "10 Search query\r\n",
+ Status: 10,
+ Meta: "Search query",
+ },
+ {
+ Raw: "30 /redirect\r\n",
+ Status: 30,
+ Meta: "/redirect",
+ },
+ {
+ Raw: "31 /redirect\r\nThis body is ignored.",
+ Status: 31,
+ Meta: "/redirect",
+ },
+ {
+ Raw: "99 Unknown status code\r\n",
+ Status: 99,
+ Meta: "Unknown status code",
+ },
+ {
+ Raw: "\r\n",
+ Err: ErrInvalidResponse,
+ },
+ {
+ Raw: "\n",
+ Err: ErrInvalidResponse,
+ },
+ {
+ Raw: "1 Bad response\r\n",
+ Err: ErrInvalidResponse,
+ },
+ {
+ Raw: "",
+ Err: io.EOF,
+ },
+ {
+ Raw: "10 Search query",
+ Err: io.EOF,
+ },
+ {
+ Raw: "20 text/gemini\nHello, world!",
+ Err: io.EOF,
+ },
+ {
+ Raw: "20 text/gemini\rHello, world!",
+ Err: ErrInvalidResponse,
+ },
+ {
+ Raw: "20 text/gemini\r",
+ Err: io.EOF,
+ },
+ {
+ Raw: "abcdefghijklmnopqrstuvwxyz",
+ Err: ErrInvalidResponse,
+ },
+ }
+
+ for _, test := range tests {
+ t.Logf("%#v", test.Raw)
+ resp, err := ReadResponse(ioutil.NopCloser(strings.NewReader(test.Raw)))
+ if err != test.Err {
+ t.Errorf("expected err = %v, got %v", test.Err, err)
+ }
+ if test.Err != nil {
+ // No response
+ continue
+ }
+ if resp.Status != test.Status {
+ t.Errorf("expected status = %d, got %d", test.Status, resp.Status)
+ }
+ if resp.Meta != test.Meta {
+ t.Errorf("expected meta = %s, got %s", test.Meta, resp.Meta)
+ }
+ b, _ := ioutil.ReadAll(resp.Body)
+ body := string(b)
+ if body != test.Body {
+ t.Errorf("expected body = %#v, got %#v", test.Body, body)
+ }
+ }
+}