github.com/wtfutil/wtf@v0.43.0/modules/urlcheck/urlResult_test.go (about)

     1  package urlcheck
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func checkValid(t *testing.T, got *urlResult) {
    10  	assert.True(t, got.IsValid)
    11  	assert.Less(t, got.ResultCode, 500)
    12  	assert.Len(t, got.ResultMessage, 0)
    13  }
    14  
    15  func checkInvalid(t *testing.T, got *urlResult) {
    16  	assert.False(t, got.IsValid)
    17  	assert.GreaterOrEqual(t, got.ResultCode, 500)
    18  	assert.Greater(t, len(got.ResultMessage), 0)
    19  }
    20  
    21  func Test_newUrlResult(t *testing.T) {
    22  	type args struct {
    23  		urlString string
    24  	}
    25  	type checks func(t *testing.T, res *urlResult)
    26  
    27  	tests := []struct {
    28  		name   string
    29  		args   args
    30  		checks checks
    31  	}{
    32  		{"good", args{"http://www.go.dev"}, checkValid},
    33  		{"good_with_page", args{"https://go.dev/doc/install"}, checkValid},
    34  		{"good_with_args", args{"https://mysite.com?var=1"}, checkValid},
    35  		{"no_url", args{""}, checkInvalid},
    36  		{"no_escape_chars", args{"http://not\nurl.com?var=1"}, checkInvalid},
    37  		{"no_protocol", args{"go.dev"}, checkInvalid},
    38  	}
    39  	for _, tt := range tests {
    40  		t.Run(tt.name, func(t *testing.T) {
    41  			if tt.checks != nil {
    42  				tt.checks(t, newUrlResult(tt.args.urlString))
    43  			}
    44  		})
    45  	}
    46  }