"Fossies" - the Fresh Open Source Software Archive

Member "geoipupdate-4.3.0/pkg/geoipupdate/geoip_updater_test.go" (16 Apr 2020, 2350 Bytes) of package /linux/misc/geoipupdate-4.3.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "geoip_updater_test.go": 4.1.5_vs_4.2.0.

    1 package geoipupdate
    2 
    3 import (
    4     "github.com/stretchr/testify/assert"
    5     "github.com/stretchr/testify/require"
    6     "io/ioutil"
    7     "net/http"
    8     "net/http/httptest"
    9     "os"
   10     "path/filepath"
   11     "testing"
   12 )
   13 
   14 func TestGetFileName(t *testing.T) {
   15     tests := []struct {
   16         Description    string
   17         FilenameStatus int
   18         FilenameBody   string
   19         ExpectedError  string
   20         ExpectedOutput string
   21     }{
   22         {
   23             Description:    "Simple Success",
   24             FilenameStatus: http.StatusOK,
   25             FilenameBody:   "aSimpleFileName",
   26             ExpectedOutput: "aSimpleFileName",
   27         },
   28         {
   29             Description:    "Get filename fails",
   30             FilenameStatus: http.StatusBadRequest,
   31             ExpectedError:  "unexpected HTTP status code: 400 Bad Request: ",
   32         },
   33         {
   34             Description:    "Get filename is missing body",
   35             FilenameStatus: http.StatusOK,
   36             ExpectedError:  "response body is empty",
   37         },
   38         {
   39             Description:    "Get filename has newlines",
   40             FilenameStatus: http.StatusOK,
   41             FilenameBody:   "bad\nfilename",
   42             ExpectedError:  "invalid characters in filename",
   43         },
   44     }
   45 
   46     tempDir, err := ioutil.TempDir("", "gutest-")
   47     require.NoError(t, err)
   48     defer func() {
   49         err = os.RemoveAll(tempDir)
   50         require.NoError(t, err)
   51     }()
   52 
   53     for _, test := range tests {
   54         t.Run(test.Description, func(t *testing.T) {
   55             server := httptest.NewServer(
   56                 http.HandlerFunc(
   57                     func(rw http.ResponseWriter, r *http.Request) {
   58                         if r.URL.Path == "/app/update_getfilename" {
   59                             rw.WriteHeader(test.FilenameStatus)
   60                             _, err := rw.Write([]byte(test.FilenameBody))
   61                             require.NoError(t, err)
   62                             return
   63                         }
   64                         if r.URL.Path == "/go-here" {
   65                             rw.WriteHeader(http.StatusNotModified)
   66                             return
   67                         }
   68                         rw.WriteHeader(http.StatusBadRequest)
   69                     },
   70                 ),
   71             )
   72 
   73             config := &Config{
   74                 AccountID:         123,
   75                 DatabaseDirectory: tempDir,
   76                 EditionIDs:        []string{"GeoIP2-City"},
   77                 LicenseKey:        "testing",
   78                 LockFile:          filepath.Join(tempDir, ".geoipupdate.lock"),
   79                 URL:               server.URL,
   80             }
   81             client := NewClient(config)
   82             actualOutput, actualError := GetFilename(config, config.EditionIDs[0], client)
   83 
   84             assert.Equal(t, test.ExpectedOutput, actualOutput, test.Description)
   85             if test.ExpectedError != "" {
   86                 require.Error(t, actualError, test.Description)
   87                 assert.Equal(t, test.ExpectedError, actualError.Error(), test.Description)
   88             }
   89         })
   90     }
   91 }