github.com/mattdotmatt/gauge@v0.3.2-0.20160421115137-425a4cdccb62/util/httpUtils_test.go (about) 1 // Copyright 2015 ThoughtWorks, Inc. 2 3 // This file is part of Gauge. 4 5 // Gauge is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 10 // Gauge is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 15 // You should have received a copy of the GNU General Public License 16 // along with Gauge. If not, see <http://www.gnu.org/licenses/>. 17 18 package util 19 20 import ( 21 "fmt" 22 "net/http" 23 "net/http/httptest" 24 "os" 25 "path/filepath" 26 "strings" 27 28 "github.com/getgauge/common" 29 . "gopkg.in/check.v1" 30 ) 31 32 func (s *MySuite) TestDownloadFailureIfFileNotFound(c *C) { 33 handler := func(w http.ResponseWriter, r *http.Request) { 34 http.NotFound(w, r) 35 } 36 37 server := httptest.NewServer(http.HandlerFunc(handler)) 38 defer server.Close() 39 40 _, err := Download(server.URL, ".", "", false) 41 42 c.Assert(err, NotNil) 43 } 44 45 func (s *MySuite) TestDownloadFailureIfServerError(c *C) { 46 handler := func(w http.ResponseWriter, r *http.Request) { 47 http.Error(w, "Internal Server Error", http.StatusInternalServerError) 48 } 49 50 server := httptest.NewServer(http.HandlerFunc(handler)) 51 defer server.Close() 52 53 _, err := Download(server.URL, ".", "", false) 54 55 c.Assert(err, NotNil) 56 } 57 58 func (s *MySuite) TestDownloadFailureIfSomeHTTPError(c *C) { 59 handler := func(w http.ResponseWriter, r *http.Request) { 60 http.Error(w, "Unauthorized", http.StatusUnauthorized) 61 } 62 63 server := httptest.NewServer(http.HandlerFunc(handler)) 64 defer server.Close() 65 66 _, err := Download(server.URL, ".", "", false) 67 68 c.Assert(err, NotNil) 69 } 70 71 func (s *MySuite) TestDownloadFailureIfTargetDirDoesntExist(c *C) { 72 handler := func(w http.ResponseWriter, r *http.Request) { 73 http.Error(w, "All OK", http.StatusOK) 74 } 75 76 server := httptest.NewServer(http.HandlerFunc(handler)) 77 defer server.Close() 78 79 _, err := Download(server.URL, "/foo/bar", "", false) 80 errMsg := fmt.Sprintf("Error downloading file: %s\nTarget dir /foo/bar doesn't exists.", server.URL) 81 82 c.Assert(err, NotNil) 83 c.Assert(err.Error(), Equals, errMsg) 84 } 85 86 func (s *MySuite) TestDownloadSuccess(c *C) { 87 os.Mkdir("temp", 0755) 88 defer os.RemoveAll("temp") 89 90 handler := func(w http.ResponseWriter, r *http.Request) { 91 http.Error(w, "All OK", http.StatusOK) 92 } 93 94 server := httptest.NewServer(http.HandlerFunc(handler)) 95 defer server.Close() 96 97 actualDownloadedFilePath, err := Download(server.URL, "temp", "", false) 98 expectedDownloadFilePath := filepath.Join("temp", strings.TrimPrefix(server.URL, "http://")) 99 absoluteDownloadFilePath, _ := filepath.Abs(expectedDownloadFilePath) 100 expectedFileContents := "All OK\n" 101 102 c.Assert(err, Equals, nil) 103 c.Assert(actualDownloadedFilePath, Equals, expectedDownloadFilePath) 104 c.Assert(common.FileExists(absoluteDownloadFilePath), Equals, true) 105 106 actualFileContents, err := common.ReadFileContents(absoluteDownloadFilePath) 107 c.Assert(err, Equals, nil) 108 c.Assert(actualFileContents, Equals, expectedFileContents) 109 }