summaryrefslogtreecommitdiff
path: root/plugins/websockets/origin_test.go
blob: bbc49bbb6ecc805e1f1881a4d2e25112027b3dd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package websockets

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestConfig_Origin(t *testing.T) {
	cfg := &Config{
		AllowedOrigin: "*",
		Broker:        "any",
	}

	err := cfg.InitDefault()
	assert.NoError(t, err)

	assert.True(t, isOriginAllowed("http://some.some.some.sssome", cfg))
	assert.True(t, isOriginAllowed("http://", cfg))
	assert.True(t, isOriginAllowed("http://google.com", cfg))
	assert.True(t, isOriginAllowed("ws://*", cfg))
	assert.True(t, isOriginAllowed("*", cfg))
	assert.True(t, isOriginAllowed("you are bad programmer", cfg)) // True :(
	assert.True(t, isOriginAllowed("****", cfg))
	assert.True(t, isOriginAllowed("asde!@#!!@#!%", cfg))
	assert.True(t, isOriginAllowed("http://*.domain.com", cfg))
}

func TestConfig_OriginWildCard(t *testing.T) {
	cfg := &Config{
		AllowedOrigin: "https://*my.site.com",
		Broker:        "any",
	}

	err := cfg.InitDefault()
	assert.NoError(t, err)

	assert.True(t, isOriginAllowed("https://my.site.com", cfg))
	assert.False(t, isOriginAllowed("http://", cfg))
	assert.False(t, isOriginAllowed("http://google.com", cfg))
	assert.False(t, isOriginAllowed("ws://*", cfg))
	assert.False(t, isOriginAllowed("*", cfg))
	assert.False(t, isOriginAllowed("you are bad programmer", cfg)) // True :(
	assert.False(t, isOriginAllowed("****", cfg))
	assert.False(t, isOriginAllowed("asde!@#!!@#!%", cfg))
	assert.False(t, isOriginAllowed("http://*.domain.com", cfg))

	assert.False(t, isOriginAllowed("https://*site.com", cfg))
	assert.True(t, isOriginAllowed("https://some.my.site.com", cfg))
}

func TestConfig_OriginWildCard2(t *testing.T) {
	cfg := &Config{
		AllowedOrigin: "https://my.*.com",
		Broker:        "any",
	}

	err := cfg.InitDefault()
	assert.NoError(t, err)

	assert.True(t, isOriginAllowed("https://my.site.com", cfg))
	assert.False(t, isOriginAllowed("http://", cfg))
	assert.False(t, isOriginAllowed("http://google.com", cfg))
	assert.False(t, isOriginAllowed("ws://*", cfg))
	assert.False(t, isOriginAllowed("*", cfg))
	assert.False(t, isOriginAllowed("you are bad programmer", cfg)) // True :(
	assert.False(t, isOriginAllowed("****", cfg))
	assert.False(t, isOriginAllowed("asde!@#!!@#!%", cfg))
	assert.False(t, isOriginAllowed("http://*.domain.com", cfg))

	assert.False(t, isOriginAllowed("https://*site.com", cfg))
	assert.True(t, isOriginAllowed("https://my.bad.com", cfg))
}