"Fossies" - the Fresh Open Source Software Archive

Member "geoipupdate-4.3.0/cmd/geoipupdate/end_to_end_test.go" (16 Apr 2020, 2469 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.

    1 package main
    2 
    3 import (
    4     "bytes"
    5     "compress/gzip"
    6     "crypto/md5"
    7     "fmt"
    8     "io"
    9     "io/ioutil"
   10     "log"
   11     "net/http"
   12     "net/http/httptest"
   13     "os"
   14     "path/filepath"
   15     "strings"
   16     "testing"
   17     "time"
   18 
   19     "github.com/maxmind/geoipupdate/v4/pkg/geoipupdate"
   20     "github.com/stretchr/testify/assert"
   21     "github.com/stretchr/testify/require"
   22 )
   23 
   24 func TestMultipleDatabaseDownload(t *testing.T) {
   25     databaseContent := "database content goes here"
   26 
   27     server := httptest.NewServer(
   28         http.HandlerFunc(
   29             func(rw http.ResponseWriter, r *http.Request) {
   30                 err := r.ParseForm()
   31                 require.NoError(t, err, "parse form")
   32 
   33                 if strings.HasPrefix(r.URL.Path, "/geoip/databases") {
   34                     buf := &bytes.Buffer{}
   35                     gzWriter := gzip.NewWriter(buf)
   36                     md5Writer := md5.New()
   37                     multiWriter := io.MultiWriter(gzWriter, md5Writer)
   38                     _, err := multiWriter.Write([]byte(
   39                         databaseContent + " " + r.URL.Path,
   40                     ))
   41                     require.NoError(t, err)
   42                     err = gzWriter.Close()
   43                     require.NoError(t, err)
   44 
   45                     rw.Header().Set(
   46                         "X-Database-MD5",
   47                         fmt.Sprintf("%x", md5Writer.Sum(nil)),
   48                     )
   49                     rw.Header().Set("Last-Modified", time.Now().Format(time.RFC1123))
   50 
   51                     _, err = rw.Write(buf.Bytes())
   52                     require.NoError(t, err)
   53 
   54                     return
   55                 }
   56 
   57                 if r.URL.Path == "/app/update_getfilename" {
   58                     _, err := rw.Write([]byte(r.Form.Get("product_id") + ".mmdb"))
   59                     require.NoError(t, err)
   60                     return
   61                 }
   62 
   63                 rw.WriteHeader(http.StatusBadRequest)
   64             },
   65         ),
   66     )
   67     defer server.Close()
   68 
   69     client := server.Client()
   70 
   71     tempDir, err := ioutil.TempDir("", "gutest-")
   72     require.NoError(t, err)
   73     defer func() {
   74         err := os.RemoveAll(tempDir)
   75         require.NoError(t, err)
   76     }()
   77 
   78     config := &geoipupdate.Config{
   79         AccountID:         123,
   80         DatabaseDirectory: tempDir,
   81         EditionIDs:        []string{"GeoLite2-City", "GeoLite2-Country"},
   82         LicenseKey:        "testing",
   83         LockFile:          filepath.Join(tempDir, ".geoipupdate.lock"),
   84         URL:               server.URL,
   85     }
   86 
   87     logOutput := &bytes.Buffer{}
   88     log.SetOutput(logOutput)
   89 
   90     err = run(client, config)
   91     assert.NoError(t, err, "run successfully")
   92 
   93     assert.Equal(t, "", logOutput.String(), "no logged output")
   94 
   95     for _, editionID := range config.EditionIDs {
   96         path := filepath.Join(config.DatabaseDirectory, editionID+".mmdb")
   97         buf, err := ioutil.ReadFile(path) //nolint:gosec
   98         require.NoError(t, err, "read file")
   99         assert.Equal(
  100             t,
  101             databaseContent+" /geoip/databases/"+editionID+"/update",
  102             string(buf),
  103             "correct database",
  104         )
  105     }
  106 }