From 1af34aa433915d6cac2efc3e37d8dc4e1cf118e0 Mon Sep 17 00:00:00 2001 From: Jingwen Owen Ou Date: Wed, 1 Jan 2014 09:39:55 -0800 Subject: [PATCH] Add test to downloadFile --- commands/updater_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 commands/updater_test.go diff --git a/commands/updater_test.go b/commands/updater_test.go new file mode 100644 index 00000000..a617bc86 --- /dev/null +++ b/commands/updater_test.go @@ -0,0 +1,30 @@ +package commands + +import ( + "fmt" + "github.com/bmizerany/assert" + "io/ioutil" + "net/http" + "net/http/httptest" + "path/filepath" + "testing" +) + +func TestUpdater_downloadFile(t *testing.T) { + mux := http.NewServeMux() + server := httptest.NewServer(mux) + defer server.Close() + + mux.HandleFunc("/gh.zip", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "GET", r.Method) + fmt.Fprint(w, "1234") + }) + + path, err := downloadFile(fmt.Sprintf("%s/gh.zip", server.URL)) + assert.Equal(t, nil, err) + + content, err := ioutil.ReadFile(path) + assert.Equal(t, nil, err) + assert.Equal(t, "1234", string(content)) + assert.Equal(t, "gh.zip", filepath.Base(path)) +}