|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +RSpec.describe WebValve::ServiceUrlConverter do |
| 4 | + let(:url) { "http://bar.com" } |
| 5 | + |
| 6 | + subject { described_class.new(url: url) } |
| 7 | + |
| 8 | + describe '#regexp' do |
| 9 | + it "returns a regexp" do |
| 10 | + expect(subject.regexp).to be_a(Regexp) |
| 11 | + end |
| 12 | + |
| 13 | + context "with a regexp" do |
| 14 | + let(:url) { %r{\Ahttp://foo\.com} } |
| 15 | + |
| 16 | + it "returns the same object" do |
| 17 | + expect(subject.regexp).to be_a(Regexp) |
| 18 | + expect(subject.regexp).to equal(url) |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + context "with an empty url" do |
| 23 | + let(:url) { "" } |
| 24 | + |
| 25 | + it "matches empty string" do |
| 26 | + expect("").to match(subject.regexp) |
| 27 | + end |
| 28 | + |
| 29 | + it "matches a string starting with a URL delimiter because the rest is just interpreted as suffix" do |
| 30 | + expect(":do:do:dodo:do:do").to match(subject.regexp) |
| 31 | + end |
| 32 | + |
| 33 | + it "doesn't match a string that doesn't start with a delimiter" do |
| 34 | + expect("jamietart:do:do:dodo:do:do").not_to match(subject.regexp) |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + context "with a boundary char on the end" do |
| 39 | + let(:url) { "http://bar.com/" } |
| 40 | + |
| 41 | + it "matches arbitrary suffixes" do |
| 42 | + expect("http://bar.com/baz/bump/beep").to match(subject.regexp) |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + context "with multiple asterisks" do |
| 47 | + let(:url) { "http://bar.com/**/bump" } |
| 48 | + |
| 49 | + it "matches like a single asterisk" do |
| 50 | + expect("http://bar.com/foo/bump").to match(subject.regexp) |
| 51 | + end |
| 52 | + |
| 53 | + it "doesn't match like a filesystem glob" do |
| 54 | + expect("http://bar.com/foo/bar/bump").not_to match(subject.regexp) |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + context "with a trailing *" do |
| 59 | + let(:url) { "http://bar.com/*" } |
| 60 | + |
| 61 | + it "matches when empty" do |
| 62 | + expect("http://bar.com/").to match(subject.regexp) |
| 63 | + end |
| 64 | + |
| 65 | + it "matches when existing" do |
| 66 | + expect("http://bar.com/foobaloo").to match(subject.regexp) |
| 67 | + end |
| 68 | + |
| 69 | + it "matches with additional tokens" do |
| 70 | + expect("http://bar.com/foobaloo/wink").to match(subject.regexp) |
| 71 | + end |
| 72 | + |
| 73 | + it "doesn't match when missing the trailing slash tho" do |
| 74 | + expect("http://bar.com").not_to match(subject.regexp) |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + context "with a totally wildcarded protocol" do |
| 79 | + let(:url) { "*://bar.com" } |
| 80 | + |
| 81 | + it "matches http" do |
| 82 | + expect("http://bar.com/").to match(subject.regexp) |
| 83 | + end |
| 84 | + |
| 85 | + it "matches anything else" do |
| 86 | + expect("gopher://bar.com/").to match(subject.regexp) |
| 87 | + end |
| 88 | + |
| 89 | + it "matches empty" do |
| 90 | + expect("://bar.com").to match(subject.regexp) |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + context "with a wildcarded partial protocol" do |
| 95 | + let(:url) { "http*://bar.com" } |
| 96 | + |
| 97 | + it "matches empty" do |
| 98 | + expect("http://bar.com/").to match(subject.regexp) |
| 99 | + end |
| 100 | + |
| 101 | + it "matches full" do |
| 102 | + expect("https://bar.com/").to match(subject.regexp) |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + context "with a TLD that is a substring of another TLD" do |
| 107 | + let(:url) { "http://bar.co" } |
| 108 | + |
| 109 | + it "doesn't match a different TLD when extending" do |
| 110 | + expect("http://bar.com").not_to match(subject.regexp) |
| 111 | + end |
| 112 | + end |
| 113 | + |
| 114 | + context "with a wildcard subdomain" do |
| 115 | + let(:url) { "http://*.bar.com" } |
| 116 | + |
| 117 | + it "matches" do |
| 118 | + expect("http://foo.bar.com").to match(subject.regexp) |
| 119 | + end |
| 120 | + |
| 121 | + it "doesn't match when too many subdomains" do |
| 122 | + expect("http://beep.foo.bar.com").not_to match(subject.regexp) |
| 123 | + end |
| 124 | + end |
| 125 | + |
| 126 | + context "with a partial postfix wildcard subdomain" do |
| 127 | + let(:url) { "http://foo*.bar.com" } |
| 128 | + |
| 129 | + it "matches when present" do |
| 130 | + expect("http://foobaz.bar.com").to match(subject.regexp) |
| 131 | + end |
| 132 | + |
| 133 | + it "matches when empty" do |
| 134 | + expect("http://foo.bar.com").to match(subject.regexp) |
| 135 | + end |
| 136 | + |
| 137 | + it "doesn't match when out of order" do |
| 138 | + expect("http://bazfoo.bar.com").not_to match(subject.regexp) |
| 139 | + end |
| 140 | + end |
| 141 | + |
| 142 | + context "with a partial prefix wildcard subdomain" do |
| 143 | + let(:url) { "http://*baz.bar.com" } |
| 144 | + |
| 145 | + it "matches when present" do |
| 146 | + expect("http://foobaz.bar.com").to match(subject.regexp) |
| 147 | + end |
| 148 | + |
| 149 | + it "matches when empty" do |
| 150 | + expect("http://baz.bar.com").to match(subject.regexp) |
| 151 | + end |
| 152 | + end |
| 153 | + |
| 154 | + context "with a wildcarded basic auth url" do |
| 155 | + let(:url) { "http://*:*@bar.com" } |
| 156 | + |
| 157 | + it "matches when present" do |
| 158 | + expect("http://bilbo:baggins@bar.com").to match(subject.regexp) |
| 159 | + end |
| 160 | + |
| 161 | + it "doesn't match when malformed" do |
| 162 | + expect("http://bilbobaggins@bar.com").not_to match(subject.regexp) |
| 163 | + end |
| 164 | + |
| 165 | + it "doesn't match when missing password part" do |
| 166 | + expect("http://bilbo@bar.com").not_to match(subject.regexp) |
| 167 | + end |
| 168 | + end |
| 169 | + |
| 170 | + context "with a wildcarded path" do |
| 171 | + let(:url) { "http://bar.com/*/whatever" } |
| 172 | + |
| 173 | + it "matches with arbitrarily spicy but legal, non-URL-significant characters" do |
| 174 | + expect("http://bar.com/a0-_~[]!$'(),;%+/whatever").to match(subject.regexp) |
| 175 | + end |
| 176 | + |
| 177 | + it "doesn't match when you throw a URL-significant char in there" do |
| 178 | + expect("http://bar.com/life=love/whatever").not_to match(subject.regexp) |
| 179 | + end |
| 180 | + end |
| 181 | + |
| 182 | + context "with a wildcarded query param" do |
| 183 | + let(:url) { "http://bar.com/whatever?foo=*&bar=bump" } |
| 184 | + |
| 185 | + it "matches when present" do |
| 186 | + expect("http://bar.com/whatever?foo=baz&bar=bump").to match(subject.regexp) |
| 187 | + end |
| 188 | + |
| 189 | + it "doesn't match when you throw a URL-significant char in there" do |
| 190 | + expect("http://bar.com/whatever?foo=baz#&bar=bump").not_to match(subject.regexp) |
| 191 | + end |
| 192 | + end |
| 193 | + end |
| 194 | +end |
0 commit comments