From a796ca03c54a6fd8338b63eb7c94b70e2cc12ad6 Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Wed, 3 May 2017 02:32:36 +0900 Subject: [PATCH] Add TestXSearch --- cmd/mstdn/cmd_xsearch_test.go | 60 +++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 cmd/mstdn/cmd_xsearch_test.go diff --git a/cmd/mstdn/cmd_xsearch_test.go b/cmd/mstdn/cmd_xsearch_test.go new file mode 100644 index 0000000..1c91aca --- /dev/null +++ b/cmd/mstdn/cmd_xsearch_test.go @@ -0,0 +1,60 @@ +package main + +import ( + "bytes" + "fmt" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +func TestXSearch(t *testing.T) { + canErr := true + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if canErr { + canErr = false + http.Error(w, http.StatusText(http.StatusInternalServerError), 9999) + return + } else if r.URL.Query().Get("q") == "empty" { + fmt.Fprintln(w, `

test status

`) + return + } + + fmt.Fprintln(w, `

test status

`) + })) + defer ts.Close() + + err := xSearch(":", "", nil) + if err == nil { + t.Fatalf("should be fail: %v", err) + } + + err = xSearch(ts.URL, "", nil) + if err == nil { + t.Fatalf("should be fail: %v", err) + } + + buf := bytes.NewBuffer(nil) + err = xSearch(ts.URL, "empty", buf) + if err != nil { + t.Fatalf("should not be fail: %v", err) + } + result := buf.String() + if result != "" { + t.Fatalf("the search result should be empty: %s", result) + } + + buf = bytes.NewBuffer(nil) + err = xSearch(ts.URL, "test", buf) + if err != nil { + t.Fatalf("should not be fail: %v", err) + } + result = buf.String() + if !strings.Contains(result, "http://example.com/@test/1") { + t.Fatalf("%q should be contained in output of search: %s", "http://example.com/@test/1", result) + } + if !strings.Contains(result, "test status") { + t.Fatalf("%q should be contained in output of search: %s", "test status", result) + } +}