@@ -10,6 +10,7 @@ import (
1010 "os"
1111 "testing"
1212 "time"
13+ "net/url"
1314)
1415
1516type HonorDecodeInStruct struct {
@@ -21,6 +22,16 @@ func (h *HonorDecodeInStruct) Decode(env string) error {
2122 return nil
2223}
2324
25+ type CustomURL struct {
26+ Value * url.URL
27+ }
28+
29+ func (cu * CustomURL ) UnmarshalBinary (data []byte ) error {
30+ u , err := url .Parse (string (data ))
31+ cu .Value = u
32+ return err
33+ }
34+
2435type Specification struct {
2536 Embedded `desc:"can we document a struct"`
2637 EmbeddedButIgnored `ignored:"true"`
@@ -53,6 +64,8 @@ type Specification struct {
5364 DecodeStruct HonorDecodeInStruct `envconfig:"honor"`
5465 Datetime time.Time
5566 MapField map [string ]string `default:"one:two,three:four"`
67+ UrlValue CustomURL
68+ UrlPointer * CustomURL
5669}
5770
5871type Embedded struct {
@@ -89,6 +102,8 @@ func TestProcess(t *testing.T) {
89102 os .Setenv ("ENV_CONFIG_HONOR" , "honor" )
90103 os .Setenv ("ENV_CONFIG_DATETIME" , "2016-08-16T18:57:05Z" )
91104 os .Setenv ("ENV_CONFIG_MULTI_WORD_VAR_WITH_AUTO_SPLIT" , "24" )
105+ os .Setenv ("ENV_CONFIG_URLVALUE" , "https://github.com/kelseyhightower/envconfig" )
106+ os .Setenv ("ENV_CONFIG_URLPOINTER" , "https://github.com/kelseyhightower/envconfig" )
92107 err := Process ("env_config" , & s )
93108 if err != nil {
94109 t .Error (err .Error ())
@@ -171,6 +186,19 @@ func TestProcess(t *testing.T) {
171186 if s .MultiWordVarWithAutoSplit != 24 {
172187 t .Errorf ("expected %q, got %q" , 24 , s .MultiWordVarWithAutoSplit )
173188 }
189+
190+ u , err := url .Parse ("https://github.com/kelseyhightower/envconfig" )
191+ if err != nil {
192+ t .Fatalf ("unexpected error: %v" , err )
193+ }
194+
195+ if * s .UrlValue .Value != * u {
196+ t .Errorf ("expected %q, got %q" , u , s .UrlValue .Value .String ())
197+ }
198+
199+ if * s .UrlPointer .Value != * u {
200+ t .Errorf ("expected %q, got %q" , u , s .UrlPointer .Value .String ())
201+ }
174202}
175203
176204func TestParseErrorBool (t * testing.T ) {
@@ -669,7 +697,7 @@ func TestTextUnmarshalerError(t *testing.T) {
669697 t .Errorf ("expected ParseError, got %v" , v )
670698 }
671699 if v .FieldName != "Datetime" {
672- t .Errorf ("expected %s, got %v" , "Debug " , v .FieldName )
700+ t .Errorf ("expected %s, got %v" , "Datetime " , v .FieldName )
673701 }
674702
675703 expectedLowLevelError := time.ParseError {
@@ -682,8 +710,34 @@ func TestTextUnmarshalerError(t *testing.T) {
682710 if v .Err .Error () != expectedLowLevelError .Error () {
683711 t .Errorf ("expected %s, got %s" , expectedLowLevelError , v .Err )
684712 }
685- if s .Debug != false {
686- t .Errorf ("expected %v, got %v" , false , s .Debug )
713+ }
714+
715+ func TestBinaryUnmarshalerError (t * testing.T ) {
716+ var s Specification
717+ os .Clearenv ()
718+ os .Setenv ("ENV_CONFIG_REQUIREDVAR" , "foo" )
719+ os .Setenv ("ENV_CONFIG_URLPOINTER" , "http://%41:8080/" )
720+
721+ err := Process ("env_config" , & s )
722+
723+ v , ok := err .(* ParseError )
724+ if ! ok {
725+ t .Fatalf ("expected ParseError, got %T %v" , err , err )
726+ }
727+ if v .FieldName != "UrlPointer" {
728+ t .Errorf ("expected %s, got %v" , "UrlPointer" , v .FieldName )
729+ }
730+
731+ // To be compatible with go 1.5 and lower we should do a very basic check,
732+ // because underlying error message varies in go 1.5 and go 1.6+.
733+
734+ ue , ok := v .Err .(* url.Error )
735+ if ! ok {
736+ t .Errorf ("expected error type to be \" *url.Error\" , got %T" , v .Err )
737+ }
738+
739+ if ue .Op != "parse" {
740+ t .Errorf ("expected error op to be \" parse\" , got %q" , ue .Op )
687741 }
688742}
689743
0 commit comments