Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 01:00 UTC
| 1 | // Copyright 2015 Matthew Holt and The Caddy Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package push |
| 15 | |
| 16 | import ( |
| 17 | "reflect" |
| 18 | "testing" |
| 19 | ) |
| 20 | |
| 21 | func TestParseLinkHeader(t *testing.T) { |
| 22 | testCases := []struct { |
| 23 | header string |
| 24 | expectedResources []linkResource |
| 25 | }{ |
| 26 | { |
| 27 | header: "</resource>; as=script", |
| 28 | expectedResources: []linkResource{{uri: "/resource", params: map[string]string{"as": "script"}}}, |
| 29 | }, |
| 30 | { |
| 31 | header: "</resource>", |
| 32 | expectedResources: []linkResource{{uri: "/resource", params: map[string]string{}}}, |
| 33 | }, |
| 34 | { |
| 35 | header: "</resource>; nopush", |
| 36 | expectedResources: []linkResource{{uri: "/resource", params: map[string]string{"nopush": "nopush"}}}, |
| 37 | }, |
| 38 | { |
| 39 | header: "</resource>;nopush;rel=next", |
| 40 | expectedResources: []linkResource{{uri: "/resource", params: map[string]string{"nopush": "nopush", "rel": "next"}}}, |
| 41 | }, |
| 42 | { |
| 43 | header: "</resource>;nopush;rel=next,</resource2>;nopush", |
| 44 | expectedResources: []linkResource{ |
| 45 | {uri: "/resource", params: map[string]string{"nopush": "nopush", "rel": "next"}}, |
| 46 | {uri: "/resource2", params: map[string]string{"nopush": "nopush"}}, |
| 47 | }, |
| 48 | }, |
| 49 | { |
| 50 | header: "</resource>,</resource2>", |
| 51 | expectedResources: []linkResource{ |
| 52 | {uri: "/resource", params: map[string]string{}}, |
| 53 | {uri: "/resource2", params: map[string]string{}}, |
| 54 | }, |
| 55 | }, |
| 56 | { |
| 57 | header: "malformed", |
| 58 | expectedResources: []linkResource{}, |
| 59 | }, |
| 60 | { |
| 61 | header: "<malformed", |
| 62 | expectedResources: []linkResource{}, |
| 63 | }, |
| 64 | { |
| 65 | header: ",", |
| 66 | expectedResources: []linkResource{}, |
| 67 | }, |
| 68 | { |
| 69 | header: ";", |
| 70 | expectedResources: []linkResource{}, |
| 71 | }, |
| 72 | { |
| 73 | header: "</resource> ; ", |
| 74 | expectedResources: []linkResource{{uri: "/resource", params: map[string]string{}}}, |
| 75 | }, |
| 76 | } |
| 77 | |
| 78 | for i, test := range testCases { |
| 79 | actualResources := parseLinkHeader(test.header) |
| 80 | if !reflect.DeepEqual(actualResources, test.expectedResources) { |
| 81 | t.Errorf("Test %d (header: %s) - expected resources %v, got %v", |
| 82 | i, test.header, test.expectedResources, actualResources) |
| 83 | } |
| 84 | } |
| 85 | } |