Skip to content

Commit ecb8d49

Browse files
committed
Add test on "rewrite" and if statements with regexps
1 parent 3337462 commit ecb8d49

File tree

16 files changed

+134
-0
lines changed

16 files changed

+134
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.github.odiszapc.nginxparser;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import java.util.Iterator;
6+
7+
import static com.github.odiszapc.nginxparser.TestUtils.assertIfBlock;
8+
9+
public class IfTest extends ParseTestBase {
10+
11+
@Test
12+
public void testC1() throws Exception {
13+
Iterator<NgxEntry> it = parse("if/c1.conf").getEntries().iterator();
14+
NgxBlock block = (NgxBlock) it.next();
15+
Iterator<NgxEntry> it2 = block.iterator();
16+
assertIfBlock(it2.next(), "if", "$request_method", "=", "POST");
17+
Assert.assertFalse(it.hasNext());
18+
}
19+
20+
@Test
21+
public void testC2() throws Exception {
22+
assertIfBlock(extractIF("if/c2.conf"), "if", "$query_string", "!=", "\"\"");
23+
}
24+
25+
@Test
26+
public void testC3() throws Exception {
27+
assertIfBlock(extractIF("if/c3.conf"), "if", "$request_uri", "!~", "\\/$");
28+
}
29+
30+
@Test
31+
public void testC4() throws Exception {
32+
assertIfBlock(extractIF("if/c4.conf"), "if", "$request_uri", "~*",
33+
"\"(sitemap(_index)?\\.xml(\\.gz)?|[a-z0-9_\\-]+-sitemap([0-9]+)?\\.xml(\\.gz)?)\"");
34+
}
35+
36+
@Test
37+
public void testC5() throws Exception {
38+
assertIfBlock(extractIF("if/c5.conf"), "if", "$request_uri", "~*",
39+
"(sitemap(_index)?\\.xml(\\.gz)?|[a-z0-9_\\-]+-sitemap([0-9]+)?\\.xml(\\.gz)?)");
40+
}
41+
42+
@Test
43+
public void testC6() throws Exception {
44+
assertIfBlock(extractIF("if/c6.conf"), "if", "-f",
45+
"\"$document_root/wp-content/cache/page_enhanced/$host/$request_uri/_index$w3tc_ua$w3tc_ref$w3tc_ssl.html$w3tc_enc\"");
46+
}
47+
48+
private NgxEntry extractIF(String path) throws Exception {
49+
Iterator<NgxEntry> it = parse(path).getEntries().iterator();
50+
NgxBlock block = (NgxBlock) it.next();
51+
Iterator<NgxEntry> it2 = block.iterator();
52+
return it2.next();
53+
}
54+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
package com.github.odiszapc.nginxparser;
22

3+
import java.util.Iterator;
4+
35
public class ParseTestBase {
46
protected NgxConfig parse(String path) throws Exception {
57
return TestUtils.parseAntlr(path);
68
}
9+
10+
protected NgxEntry getFirstParam(String path) throws Exception {
11+
Iterator<NgxEntry> it = parse(path).getEntries().iterator();
12+
return it.next();
13+
}
714
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.github.odiszapc.nginxparser;
2+
3+
import org.junit.Test;
4+
5+
public class RewriteTest extends ParseTestBase {
6+
7+
@Test
8+
public void testC1() throws Exception {
9+
TestUtils.assertParam(getFirstParam("rewrite/c1.conf"), "rewrite", "^/dashboard$", "/admin.php?controller=user&action=login", "last");
10+
}
11+
12+
@Test
13+
public void testС2() throws Exception {
14+
TestUtils.assertParam(getFirstParam("rewrite/c2.conf"), "rewrite", "^/feed", "/feed.php", "last");
15+
}
16+
17+
@Test
18+
public void testС3() throws Exception {
19+
TestUtils.assertParam(getFirstParam("rewrite/c3.conf"), "rewrite", "^/category/([^/]+)/page-([0-9]+)$", "/index.php?controller=blog&action=view&category=$1&page=$2", "last");
20+
}
21+
22+
@Test
23+
public void testС4() throws Exception {
24+
TestUtils.assertParam(getFirstParam("rewrite/c4.conf"), "rewrite", "^/category/([^/]+)/$", "/index.php?controller=blog&action=view&category=$1&page=0", "last");
25+
}
26+
27+
@Test
28+
public void testС5() throws Exception {
29+
TestUtils.assertParam(getFirstParam("rewrite/c5.conf"), "rewrite", "^/page-([0-9]+)$", "/index.php?controller=blog&action=view&page=$1", "last");
30+
}
31+
32+
@Test
33+
public void testС6() throws Exception {
34+
TestUtils.assertParam(getFirstParam("rewrite/c6.conf"), "rewrite", "^", "/index.php?controller=post&action=view&id_post=$1", "last");
35+
}
36+
}

src/test/java/com/github/odiszapc/nginxparser/TestUtils.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,19 @@ public static NgxBlock assertBlock(NgxEntry entry, String name, String... values
6565
return (NgxBlock) entry;
6666
}
6767

68+
public static NgxIfBlock assertIfBlock(NgxEntry entry, String name, String... values) {
69+
Assert.assertTrue(entry instanceof NgxBlock);
70+
Assert.assertEquals(((NgxIfBlock) entry).getName(), name);
71+
72+
Assert.assertEquals(values.length, ((NgxBlock) entry).getValues().size());
73+
Iterator<String> it = ((NgxBlock) entry).getValues().iterator();
74+
for (String value : values) {
75+
Assert.assertEquals(it.next(), value);
76+
}
77+
78+
return (NgxIfBlock) entry;
79+
}
80+
6881
@SuppressWarnings("UnusedReturnValue")
6982
public static NgxComment assertComment(NgxEntry entry, @SuppressWarnings("SameParameterValue") String value) {
7083
Assert.assertTrue(entry instanceof NgxComment);

src/test/resources/if/c1.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
block {
2+
if ($request_method = POST) { param 0;}
3+
}

src/test/resources/if/c2.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
block {
2+
if ($query_string != "") { param 0;}
3+
}

src/test/resources/if/c3.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
block {
2+
if ($request_uri !~ \/$) { param 0;}
3+
}

src/test/resources/if/c4.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
block {
2+
if ($request_uri ~* "(sitemap(_index)?\.xml(\.gz)?|[a-z0-9_\-]+-sitemap([0-9]+)?\.xml(\.gz)?)") { param 0;}
3+
}

src/test/resources/if/c5.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
block {
2+
if ($request_uri ~* (sitemap(_index)?\.xml(\.gz)?|[a-z0-9_\-]+-sitemap([0-9]+)?\.xml(\.gz)?)) { param 0;}
3+
}

src/test/resources/if/c6.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
block {
2+
if (-f "$document_root/wp-content/cache/page_enhanced/$host/$request_uri/_index$w3tc_ua$w3tc_ref$w3tc_ssl.html$w3tc_enc") { param 0;}
3+
}

src/test/resources/rewrite/c1.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rewrite ^/dashboard$ /admin.php?controller=user&action=login last;

src/test/resources/rewrite/c2.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rewrite ^/feed /feed.php last;

src/test/resources/rewrite/c3.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rewrite ^/category/([^/]+)/page-([0-9]+)$ /index.php?controller=blog&action=view&category=$1&page=$2 last;

src/test/resources/rewrite/c4.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rewrite ^/category/([^/]+)/$ /index.php?controller=blog&action=view&category=$1&page=0 last;

src/test/resources/rewrite/c5.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rewrite ^/page-([0-9]+)$ /index.php?controller=blog&action=view&page=$1 last;

src/test/resources/rewrite/c6.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rewrite ^ /index.php?controller=post&action=view&id_post=$1 last;

0 commit comments

Comments
 (0)