github.com/getgauge/gauge@v1.6.9/util/httpUtils_test.go (about)

     1  /*----------------------------------------------------------------
     2   *  Copyright (c) ThoughtWorks, Inc.
     3   *  Licensed under the Apache License, Version 2.0
     4   *  See LICENSE in the project root for license information.
     5   *----------------------------------------------------------------*/
     6  
     7  package util
     8  
     9  import (
    10  	"fmt"
    11  	"net/http"
    12  	"net/http/httptest"
    13  	"os"
    14  	"path/filepath"
    15  	"strings"
    16  
    17  	"github.com/getgauge/common"
    18  	. "gopkg.in/check.v1"
    19  )
    20  
    21  func (s *MySuite) TestDownloadFailureIfFileNotFound(c *C) {
    22  	handler := func(w http.ResponseWriter, r *http.Request) {
    23  		http.NotFound(w, r)
    24  	}
    25  
    26  	server := httptest.NewServer(http.HandlerFunc(handler))
    27  	defer server.Close()
    28  
    29  	_, err := Download(server.URL, ".", "", false)
    30  
    31  	c.Assert(err, NotNil)
    32  }
    33  
    34  func (s *MySuite) TestDownloadFailureIfServerError(c *C) {
    35  	handler := func(w http.ResponseWriter, r *http.Request) {
    36  		http.Error(w, "Internal Server Error", http.StatusInternalServerError)
    37  	}
    38  
    39  	server := httptest.NewServer(http.HandlerFunc(handler))
    40  	defer server.Close()
    41  
    42  	_, err := Download(server.URL, ".", "", false)
    43  
    44  	c.Assert(err, NotNil)
    45  }
    46  
    47  func (s *MySuite) TestDownloadFailureIfSomeHTTPError(c *C) {
    48  	handler := func(w http.ResponseWriter, r *http.Request) {
    49  		http.Error(w, "Unauthorized", http.StatusUnauthorized)
    50  	}
    51  
    52  	server := httptest.NewServer(http.HandlerFunc(handler))
    53  	defer server.Close()
    54  
    55  	_, err := Download(server.URL, ".", "", false)
    56  
    57  	c.Assert(err, NotNil)
    58  }
    59  
    60  func (s *MySuite) TestDownloadFailureIfTargetDirDoesntExist(c *C) {
    61  	handler := func(w http.ResponseWriter, r *http.Request) {
    62  		http.Error(w, "All OK", http.StatusOK)
    63  	}
    64  
    65  	server := httptest.NewServer(http.HandlerFunc(handler))
    66  	defer server.Close()
    67  
    68  	_, err := Download(server.URL, "/foo/bar", "", false)
    69  	errMsg := fmt.Sprintf("Error downloading file: %s\nTarget dir /foo/bar doesn't exists.", server.URL)
    70  
    71  	c.Assert(err, NotNil)
    72  	c.Assert(err.Error(), Equals, errMsg)
    73  }
    74  
    75  func (s *MySuite) TestDownloadSuccess(c *C) {
    76  	err := os.Mkdir("temp", 0755)
    77  	c.Assert(err, IsNil)
    78  	defer os.RemoveAll("temp")
    79  
    80  	handler := func(w http.ResponseWriter, r *http.Request) {
    81  		http.Error(w, "All OK", http.StatusOK)
    82  	}
    83  
    84  	server := httptest.NewServer(http.HandlerFunc(handler))
    85  	defer server.Close()
    86  
    87  	actualDownloadedFilePath, err := Download(server.URL, "temp", "", false)
    88  	expectedDownloadFilePath := filepath.Join("temp", strings.TrimPrefix(server.URL, "http://"))
    89  	absoluteDownloadFilePath, _ := filepath.Abs(expectedDownloadFilePath)
    90  	expectedFileContents := "All OK\n"
    91  
    92  	c.Assert(err, Equals, nil)
    93  	c.Assert(actualDownloadedFilePath, Equals, expectedDownloadFilePath)
    94  	c.Assert(common.FileExists(absoluteDownloadFilePath), Equals, true)
    95  
    96  	actualFileContents, err := common.ReadFileContents(absoluteDownloadFilePath)
    97  	c.Assert(err, Equals, nil)
    98  	c.Assert(actualFileContents, Equals, expectedFileContents)
    99  }