From c7378e2ae04f25d1f09346043f1dbb96b2718fec Mon Sep 17 00:00:00 2001 From: "Stephen McQuay (work)" Date: Fri, 12 May 2017 13:44:54 -0700 Subject: [PATCH] added url fetching --- fetch.go | 26 ++++++++++++++++++++++++++ fetch_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 fetch.go create mode 100644 fetch_test.go diff --git a/fetch.go b/fetch.go new file mode 100644 index 0000000..4dc7a8f --- /dev/null +++ b/fetch.go @@ -0,0 +1,26 @@ +package hmm + +import ( + "bufio" + "net/http" +) + +// Lines returns a chan containing one line of output from the remote url. +func Lines(url string) <-chan string { + r := make(chan string) + + go func() { + defer close(r) + resp, err := http.Get(url) + if err != nil { + return + } + s := bufio.NewScanner(resp.Body) + for s.Scan() { + r <- s.Text() + } + // ignore errors + }() + + return r +} diff --git a/fetch_test.go b/fetch_test.go new file mode 100644 index 0000000..ebb36cc --- /dev/null +++ b/fetch_test.go @@ -0,0 +1,27 @@ +package hmm + +import ( + "fmt" + "net/http" + "net/http/httptest" + "testing" +) + +type dummy struct{} + +func (d *dummy) ServeHTTP(w http.ResponseWriter, r *http.Request) { + for i := 0; i < 10; i++ { + fmt.Fprintf(w, "here is a line: %d\n", i) + } +} + +func TestFetch(t *testing.T) { + ts := httptest.NewServer(&dummy{}) + i := 0 + for range Lines(ts.URL) { + i++ + } + if got, want := i, 10; got != want { + t.Fatalf("got %d, want %d", got, want) + } +}