extend tests for tricky cases

This commit is contained in:
Umputun 2021-04-27 01:57:04 -05:00
parent 66eb3ffe4a
commit 604391e55e
3 changed files with 20 additions and 3 deletions

View File

@ -199,11 +199,13 @@ func (s *Service) extendMapper(m URLMapper) URLMapper {
src := m.SrcMatch.String()
m.Dst = strings.Replace(m.Dst, "@", "$", -1) // allow group defined as @n instead of $n (yaml friendly)
// static match with assets uses AssetsWebRoot and AssetsLocation
if m.MatchType == MTStatic && m.AssetsWebRoot != "" && m.AssetsLocation != "" {
m.AssetsWebRoot = strings.TrimSuffix(m.AssetsWebRoot, "/")
m.AssetsLocation = strings.TrimSuffix(m.AssetsLocation, "/") + "/"
}
// static match without assets defined defaulted to src:dst/
if m.MatchType == MTStatic && m.AssetsWebRoot == "" && m.AssetsLocation == "" {
m.AssetsWebRoot = strings.TrimSuffix(src, "/")
m.AssetsLocation = strings.TrimSuffix(m.Dst, "/") + "/"

View File

@ -2,6 +2,7 @@ package discovery
import (
"context"
"errors"
"regexp"
"strconv"
"testing"
@ -38,7 +39,17 @@ func TestService_Run(t *testing.T) {
}, nil
},
}
svc := NewService([]Provider{p1, p2}, time.Millisecond*10)
p3 := &ProviderMock{
EventsFunc: func(ctx context.Context) <-chan ProviderID {
return make(chan ProviderID, 1)
},
ListFunc: func() ([]URLMapper, error) {
return nil, errors.New("failed")
},
}
svc := NewService([]Provider{p1, p2, p3}, time.Millisecond*10)
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel()
@ -85,6 +96,7 @@ func TestService_Match(t *testing.T) {
AssetsWebRoot: "/web", AssetsLocation: "/var/web"},
{SrcMatch: *regexp.MustCompile("/www/"), Dst: "/var/web", ProviderID: PIDocker, MatchType: MTStatic,
AssetsWebRoot: "/www", AssetsLocation: "/var/web"},
{SrcMatch: *regexp.MustCompile("/path/"), Dst: "/var/web/path", ProviderID: PIDocker, MatchType: MTStatic},
}, nil
},
}
@ -95,7 +107,7 @@ func TestService_Match(t *testing.T) {
err := svc.Run(ctx)
require.Error(t, err)
assert.Equal(t, context.DeadlineExceeded, err)
assert.Equal(t, 5, len(svc.Mappers()))
assert.Equal(t, 6, len(svc.Mappers()))
tbl := []struct {
server, src string
@ -115,6 +127,7 @@ func TestService_Match(t *testing.T) {
{"m1.example.com", "/www/something", "/www:/var/web/", MTStatic, true},
{"m1.example.com", "/www/", "/www:/var/web/", MTStatic, true},
{"m1.example.com", "/www", "/www:/var/web/", MTStatic, true},
{"xyx.example.com", "/path/something", "/path:/var/web/path/", MTStatic, true},
}
for i, tt := range tbl {

View File

@ -93,8 +93,10 @@ func TestMakeCacheControl(t *testing.T) {
err error
}{
{nil, time.Duration(0), nil, nil},
{[]string{"12d"}, 12 * 24 * time.Hour, nil, nil},
{[]string{"12h"}, 12 * time.Hour, nil, nil},
{[]string{"12d"}, 12 * 24 * time.Hour, nil, nil},
{[]string{"a12d"}, 0, nil,
errors.New(`can't parse default cache duration: can't parse "a12d" as duration: strconv.Atoi: parsing "a12": invalid syntax`)},
{[]string{"default:12h"}, 12 * time.Hour, nil, nil},
{[]string{"blah:12h"}, 0, nil, errors.New("first cache duration has to be for the default mime")},
{[]string{"a12nop"}, 0, nil, errors.New(`can't parse default cache duration: time: invalid duration "a12nop"`)},