mirror of
https://github.com/umputun/reproxy.git
synced 2025-07-06 22:15:41 +02:00
extend tests for tricky cases
This commit is contained in:
@ -199,11 +199,13 @@ func (s *Service) extendMapper(m URLMapper) URLMapper {
|
|||||||
src := m.SrcMatch.String()
|
src := m.SrcMatch.String()
|
||||||
m.Dst = strings.Replace(m.Dst, "@", "$", -1) // allow group defined as @n instead of $n (yaml friendly)
|
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 != "" {
|
if m.MatchType == MTStatic && m.AssetsWebRoot != "" && m.AssetsLocation != "" {
|
||||||
m.AssetsWebRoot = strings.TrimSuffix(m.AssetsWebRoot, "/")
|
m.AssetsWebRoot = strings.TrimSuffix(m.AssetsWebRoot, "/")
|
||||||
m.AssetsLocation = strings.TrimSuffix(m.AssetsLocation, "/") + "/"
|
m.AssetsLocation = strings.TrimSuffix(m.AssetsLocation, "/") + "/"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// static match without assets defined defaulted to src:dst/
|
||||||
if m.MatchType == MTStatic && m.AssetsWebRoot == "" && m.AssetsLocation == "" {
|
if m.MatchType == MTStatic && m.AssetsWebRoot == "" && m.AssetsLocation == "" {
|
||||||
m.AssetsWebRoot = strings.TrimSuffix(src, "/")
|
m.AssetsWebRoot = strings.TrimSuffix(src, "/")
|
||||||
m.AssetsLocation = strings.TrimSuffix(m.Dst, "/") + "/"
|
m.AssetsLocation = strings.TrimSuffix(m.Dst, "/") + "/"
|
||||||
|
@ -2,6 +2,7 @@ package discovery
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
@ -38,7 +39,17 @@ func TestService_Run(t *testing.T) {
|
|||||||
}, nil
|
}, 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)
|
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
@ -85,6 +96,7 @@ func TestService_Match(t *testing.T) {
|
|||||||
AssetsWebRoot: "/web", AssetsLocation: "/var/web"},
|
AssetsWebRoot: "/web", AssetsLocation: "/var/web"},
|
||||||
{SrcMatch: *regexp.MustCompile("/www/"), Dst: "/var/web", ProviderID: PIDocker, MatchType: MTStatic,
|
{SrcMatch: *regexp.MustCompile("/www/"), Dst: "/var/web", ProviderID: PIDocker, MatchType: MTStatic,
|
||||||
AssetsWebRoot: "/www", AssetsLocation: "/var/web"},
|
AssetsWebRoot: "/www", AssetsLocation: "/var/web"},
|
||||||
|
{SrcMatch: *regexp.MustCompile("/path/"), Dst: "/var/web/path", ProviderID: PIDocker, MatchType: MTStatic},
|
||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -95,7 +107,7 @@ func TestService_Match(t *testing.T) {
|
|||||||
err := svc.Run(ctx)
|
err := svc.Run(ctx)
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
assert.Equal(t, context.DeadlineExceeded, err)
|
assert.Equal(t, context.DeadlineExceeded, err)
|
||||||
assert.Equal(t, 5, len(svc.Mappers()))
|
assert.Equal(t, 6, len(svc.Mappers()))
|
||||||
|
|
||||||
tbl := []struct {
|
tbl := []struct {
|
||||||
server, src string
|
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/something", "/www:/var/web/", MTStatic, true},
|
||||||
{"m1.example.com", "/www/", "/www:/var/web/", MTStatic, true},
|
{"m1.example.com", "/www/", "/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 {
|
for i, tt := range tbl {
|
||||||
|
@ -93,8 +93,10 @@ func TestMakeCacheControl(t *testing.T) {
|
|||||||
err error
|
err error
|
||||||
}{
|
}{
|
||||||
{nil, time.Duration(0), nil, nil},
|
{nil, time.Duration(0), nil, nil},
|
||||||
{[]string{"12d"}, 12 * 24 * time.Hour, nil, nil},
|
|
||||||
{[]string{"12h"}, 12 * 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{"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{"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"`)},
|
{[]string{"a12nop"}, 0, nil, errors.New(`can't parse default cache duration: time: invalid duration "a12nop"`)},
|
||||||
|
Reference in New Issue
Block a user