Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 41fe34a65a9c5efde052518b2a347459b8106c40 (plain) (blame)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//
//  ========================================================================
//  Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.rewrite.handler;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.util.Iterator;

import org.junit.Before;
import org.junit.Test;

public class HeaderRegexRuleTest extends AbstractRuleTestCase
{

    private HeaderRegexRule _rule;

    @Before
    public void init() throws Exception
    {
        start(false);
        _rule = new HeaderRegexRule();
        _rule.setRegex("\\*");
    }

    @Test
    public void testHeaderWithTextValues() throws IOException
    {
        // different keys
        String headers[][] =
        {
        { "hnum#1", "test1" },
        { "hnum#2", "2test2" },
        { "hnum#3", "test3" } };
        assertHeaders(headers);
    }

    @Test
    public void testHeaderWithNumberValues() throws IOException
    {
        String headers[][] =
        {
        { "hello", "1" },
        { "hello", "-1" },
        { "hello", "100" },
        { "hello", "100" },
        { "hello", "100" },
        { "hello", "100" },
        { "hello", "100" },
        { "hello1", "200" } };
        assertHeaders(headers);
    }

    @Test
    public void testHeaderOverwriteValues() throws IOException
    {
        String headers[][] =
        {
        { "size", "100" },
        { "size", "200" },
        { "size", "300" },
        { "size", "400" },
        { "size", "500" },
        { "title", "abc" },
        { "title", "bac" },
        { "title", "cba" },
        { "title1", "abba" },
        { "title1", "abba1" },
        { "title1", "abba" },
        { "title1", "abba1" } };
        assertHeaders(headers);
        Iterator<String> e = _response.getHeaders("size").iterator();
        int count = 0;
        while (e.hasNext())
        {
            e.next();
            count++;
        }
        assertEquals(1,count);
        assertEquals("500",_response.getHeader("size"));
        assertEquals("cba",_response.getHeader("title"));
        assertEquals("abba1",_response.getHeader("title1"));
    }

    @Test
    public void testMatch() throws Exception
    {
        _rule.setRegex("/my/dir/file/(.*)$");
        _rule.setName("cache-control");
        _rule.setValue("no-store");
        _rule.matchAndApply("/my/dir/file/",_request,_response);
        assertEquals("no-store",_response.getHeader("cache-control"));
    }

    @Test
    public void testNotMatch() throws Exception
    {
        reset();
        _rule.setRegex("/my/dir/file/(.*)$");
        _rule.setName("cache-control");
        _rule.setValue("no-store");
        _rule.matchAndApply("/my/dir/file_not_match/",_request,_response);
        assertEquals(null,_response.getHeader("cache-control"));
    }

    private void assertHeaders(String headers[][]) throws IOException
    {
        for (String[] header : headers)
        {
            _rule.setName(header[0]);
            _rule.setValue(header[1]);
            _rule.apply(null,_request,_response,null);
            assertEquals(header[1],_response.getHeader(header[0]));
        }
    }
}

Back to the top