...
1
2
3 package envconfig
4
5 import (
6 "errors"
7 "net/url"
8 "os"
9 "testing"
10 )
11
12 type SpecWithURL struct {
13 UrlValue url.URL
14 UrlPointer *url.URL
15 }
16
17 func TestParseURL(t *testing.T) {
18 var s SpecWithURL
19
20 os.Clearenv()
21 os.Setenv("ENV_CONFIG_URLVALUE", "https://github.com/kelseyhightower/envconfig")
22 os.Setenv("ENV_CONFIG_URLPOINTER", "https://github.com/kelseyhightower/envconfig")
23
24 err := Process("env_config", &s)
25 if err != nil {
26 t.Fatal("unexpected error:", err)
27 }
28
29 u, err := url.Parse("https://github.com/kelseyhightower/envconfig")
30 if err != nil {
31 t.Fatalf("unexpected error: %v", err)
32 }
33
34 if s.UrlValue != *u {
35 t.Errorf("expected %q, got %q", u, s.UrlValue.String())
36 }
37
38 if *s.UrlPointer != *u {
39 t.Errorf("expected %q, got %q", u, s.UrlPointer)
40 }
41 }
42
43 func TestParseURLError(t *testing.T) {
44 var s SpecWithURL
45
46 os.Clearenv()
47 os.Setenv("ENV_CONFIG_URLPOINTER", "http_://foo")
48
49 err := Process("env_config", &s)
50
51 v, ok := err.(*ParseError)
52 if !ok {
53 t.Fatalf("expected ParseError, got %T %v", err, err)
54 }
55 if v.FieldName != "UrlPointer" {
56 t.Errorf("expected %s, got %v", "UrlPointer", v.FieldName)
57 }
58
59 expectedUnerlyingError := url.Error{
60 Op: "parse",
61 URL: "http_://foo",
62 Err: errors.New("first path segment in URL cannot contain colon"),
63 }
64
65 if v.Err.Error() != expectedUnerlyingError.Error() {
66 t.Errorf("expected %q, got %q", expectedUnerlyingError, v.Err)
67 }
68 }
69
View as plain text