make UrlResolver reuse commons Http redirectory strategy
[teamcity/dotNetPackagesSupport.git] / nuget-tests / src / jetbrains / buildServer / nuget / tests / server / feed / reader / UrlResolverTest.java
1 /*\r
2  * Copyright 2000-2011 JetBrains s.r.o.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  * http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package jetbrains.buildServer.nuget.tests.server.feed.reader;\r
18 \r
19 import com.intellij.openapi.util.Pair;\r
20 import jetbrains.buildServer.BaseTestCase;\r
21 import jetbrains.buildServer.nuget.server.feed.reader.impl.FeedClient;\r
22 import jetbrains.buildServer.nuget.server.feed.reader.impl.FeedGetMethodFactory;\r
23 import jetbrains.buildServer.nuget.server.feed.reader.impl.UrlResolver;\r
24 import jetbrains.buildServer.nuget.server.feed.reader.impl.UrlResolverImpl;\r
25 import org.apache.http.HttpResponse;\r
26 import org.apache.http.HttpStatus;\r
27 import org.apache.http.HttpVersion;\r
28 import org.apache.http.client.methods.HttpUriRequest;\r
29 import org.apache.http.message.BasicHttpResponse;\r
30 import org.hamcrest.BaseMatcher;\r
31 import org.hamcrest.Description;\r
32 import org.jetbrains.annotations.NotNull;\r
33 import org.jmock.Expectations;\r
34 import org.jmock.Mockery;\r
35 import org.testng.Assert;\r
36 import org.testng.annotations.BeforeMethod;\r
37 import org.testng.annotations.Test;\r
38 \r
39 import java.io.IOException;\r
40 \r
41 /**\r
42  * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
43  * Date: 12.08.11 15:26\r
44  */\r
45 public class UrlResolverTest extends BaseTestCase {\r
46   private Mockery m;\r
47   private FeedClient myFeedClient;\r
48   private UrlResolver myResolver;\r
49 \r
50   @BeforeMethod\r
51   @Override\r
52   protected void setUp() throws Exception {\r
53     super.setUp();\r
54     m = new Mockery();\r
55     myFeedClient = m.mock(FeedClient.class);\r
56     myResolver = new UrlResolverImpl(myFeedClient, new FeedGetMethodFactory());\r
57   }\r
58 \r
59   @Test\r
60   public void test_should_support_200() throws IOException {\r
61     m.checking(new Expectations() {{\r
62       oneOf(myFeedClient).execute(with(httpGet("http://www.jetbrains.com")));\r
63       will(returnValue(responseStatus(200)));\r
64     }});\r
65 \r
66     final Pair<String, HttpResponse> pair = myResolver.resolvePath("http://www.jetbrains.com");\r
67     Assert.assertEquals(pair.first, "http://www.jetbrains.com");\r
68   }\r
69 \r
70   @Test\r
71   public void test_should_support_3xx() throws IOException {\r
72     m.checking(new Expectations() {{\r
73       oneOf(myFeedClient).execute(with(httpGet("http://www.jetbrains.com")));\r
74       will(returnValue(responseLocationStatus(HttpStatus.SC_MOVED_PERMANENTLY, "http://www.google.com")));\r
75       oneOf(myFeedClient).execute(with(httpGet("http://www.google.com")));\r
76       will(returnValue(responseStatus(200)));\r
77     }});\r
78 \r
79     final Pair<String, HttpResponse> pair = myResolver.resolvePath("http://www.jetbrains.com");\r
80     Assert.assertEquals(pair.first, "http://www.google.com");\r
81   }\r
82 \r
83   @Test\r
84   public void test_should_support_3xx_ms() throws IOException {\r
85     m.checking(new Expectations() {{\r
86       oneOf(myFeedClient).execute(with(httpGet("http://www.jetbrains.com/redirect?fwLink=555")));\r
87       will(returnValue(responseLocationStatus(HttpStatus.SC_MOVED_PERMANENTLY, "http://www.google.com")));\r
88       oneOf(myFeedClient).execute(with(httpGet("http://www.google.com")));\r
89       will(returnValue(responseStatus(200)));\r
90     }});\r
91 \r
92     final Pair<String, HttpResponse> pair = myResolver.resolvePath("http://www.jetbrains.com/redirect?fwLink=555");\r
93     Assert.assertEquals(pair.first, "http://www.google.com");\r
94   }\r
95 \r
96   @Test\r
97   public void test_should_support_3xx_multi() throws IOException {\r
98     m.checking(new Expectations() {{\r
99       oneOf(myFeedClient).execute(with(httpGet("http://www.jetbrains.com")));\r
100       will(returnValue(responseLocationStatus(HttpStatus.SC_MOVED_PERMANENTLY, "http://domain_1.jonnyzzz.com")));\r
101 \r
102       oneOf(myFeedClient).execute(with(httpGet("http://domain_1.jonnyzzz.com")));\r
103       will(returnValue(responseLocationStatus(HttpStatus.SC_MOVED_TEMPORARILY, "http://domain_2.jonnyzzz.com")));\r
104 \r
105       oneOf(myFeedClient).execute(with(httpGet("http://domain_2.jonnyzzz.com")));\r
106       will(returnValue(responseLocationStatus(HttpStatus.SC_TEMPORARY_REDIRECT, "http://domain_3.jonnyzzz.com")));\r
107 \r
108       oneOf(myFeedClient).execute(with(httpGet("http://domain_3.jonnyzzz.com")));\r
109       will(returnValue(responseLocationStatus(HttpStatus.SC_MOVED_PERMANENTLY, "http://domain_4.jonnyzzz.com")));\r
110 \r
111       oneOf(myFeedClient).execute(with(httpGet("http://domain_4.jonnyzzz.com")));\r
112       will(returnValue(responseLocationStatus(HttpStatus.SC_MOVED_PERMANENTLY, "http://www.google.com")));\r
113 \r
114       oneOf(myFeedClient).execute(with(httpGet("http://www.google.com")));\r
115       will(returnValue(responseStatus(200)));\r
116     }});\r
117 \r
118     final Pair<String, HttpResponse> pair = myResolver.resolvePath("http://www.jetbrains.com");\r
119     Assert.assertEquals(pair.first, "http://www.google.com");\r
120   }\r
121 \r
122   @Test\r
123   public void test_should_support_3xx_loop() throws IOException {\r
124     m.checking(new Expectations() {{\r
125       allowing(myFeedClient).execute(with(httpGet("http://www.jetbrains.com")));\r
126       will(returnValue(responseLocationStatus(HttpStatus.SC_MOVED_PERMANENTLY, "http://www.jetbrains.com")));\r
127     }});\r
128 \r
129     try {\r
130       myResolver.resolvePath("http://www.jetbrains.com");\r
131     } catch (IOException e) {\r
132       return;\r
133     }\r
134     Assert.fail();\r
135   }\r
136 \r
137   @Test\r
138   public void test_should_fail_on_500() throws IOException {\r
139     m.checking(new Expectations() {{\r
140       oneOf(myFeedClient).execute(with(httpGet("http://www.jetbrains.com")));\r
141       will(returnValue(responseStatus(500)));\r
142     }});\r
143 \r
144     try {\r
145       myResolver.resolvePath("http://www.jetbrains.com");\r
146     } catch (IOException e) {\r
147       return;\r
148     }\r
149     Assert.fail();\r
150   }\r
151 \r
152   @Test\r
153   public void test_should_fail_on_400() throws IOException {\r
154     m.checking(new Expectations() {{\r
155       oneOf(myFeedClient).execute(with(httpGet("http://www.jetbrains.com")));\r
156       will(returnValue(responseStatus(400)));\r
157     }});\r
158 \r
159     try {\r
160       myResolver.resolvePath("http://www.jetbrains.com");\r
161     } catch (IOException e) {\r
162       return;\r
163     }\r
164     Assert.fail();\r
165   }\r
166 \r
167   private static HttpResponse responseStatus(int status) {\r
168     return new BasicHttpResponse(HttpVersion.HTTP_1_0, status, "Status: " + status);\r
169   }\r
170 \r
171   private static HttpResponse responseLocationStatus(int status, @NotNull String location) {\r
172     final BasicHttpResponse res = new BasicHttpResponse(HttpVersion.HTTP_1_0, status, "Status: " + status);\r
173     res.addHeader("Location", location);\r
174     return res;\r
175   }\r
176 \r
177   private static BaseMatcher<HttpUriRequest> httpGet(@NotNull final String url) {\r
178     return new BaseMatcher<HttpUriRequest>() {\r
179       public boolean matches(Object o) {\r
180         if (!(o instanceof org.apache.http.client.methods.HttpGet)) return false;\r
181         org.apache.http.client.methods.HttpGet get = (org.apache.http.client.methods.HttpGet) o;\r
182         return get.getURI().toString().equals(url);\r
183       }\r
184 \r
185       public void describeTo(Description description) {\r
186         description.appendText("HttpGet to ").appendText(url);\r
187       }\r
188     };\r
189   }\r
190 }\r