123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- package knmi
- import (
- "encoding/xml"
- "math/rand"
- "net/url"
- "testing"
- "time"
- )
- func TestFetch(t *testing.T) {
- p := plugin{}
- p.config.ForecastURL = "ftp://ftp.knmi.nl/pub_weerberichten/basisverwachting.xml"
- u, _ := url.Parse(p.config.ForecastURL)
- t.Run("wrongdomain", func(t *testing.T) {
- fu2 := u.Scheme + "://wrongdomain." + randomword() + ".com" + u.Path
- _, err := p.fetch(fu2)
- if err == nil {
- t.Errorf("Using %s, expected error, got nil", fu2)
- }
- })
- t.Run("wrongpath", func(t *testing.T) {
- fu2 := u.Scheme + "://" + u.Host + "/" + randomword() + ".wrongpath"
- _, err := p.fetch(fu2)
- if err == nil {
- t.Errorf("Using %s, expected error, got nil", fu2)
- }
- })
- t.Run("good", func(t *testing.T) {
- // fetch from valid source
- data, err := p.fetch(p.config.ForecastURL)
- if err != nil {
- t.Errorf("Using %s, expected nil, got err: %s", p.config.ForecastURL, err)
- }
- // parse data that is supposed to be valid
- fc := knmiForecast{}
- err = xml.Unmarshal(data, &fc)
- if err != nil {
- t.Errorf("Expected nil, got err from parsing fetched data: %s", err)
- }
- })
- }
- func randomword() string {
- var word [8]byte
- const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- rng := rand.New(rand.NewSource(time.Now().UnixNano()))
- for i := 0; i < len(word); i++ {
- word[i] = letters[rng.Intn(len(letters))]
- }
- return string(word[:])
- }
|