Ray Milkey | 2287d88 | 2015-01-30 10:15:20 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.rest; |
| 17 | |
| 18 | import org.junit.Test; |
| 19 | |
| 20 | import com.sun.jersey.api.client.UniformInterfaceException; |
| 21 | import com.sun.jersey.api.client.WebResource; |
| 22 | |
| 23 | import static org.hamcrest.Matchers.containsString; |
| 24 | import static org.junit.Assert.assertThat; |
| 25 | import static org.junit.Assert.fail; |
| 26 | |
| 27 | /** |
| 28 | * Unit tests for bad REST requests. |
| 29 | */ |
| 30 | public class BadRequestTest extends ResourceTest { |
Ray Milkey | a0cecdc | 2015-04-09 09:51:52 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Tests the response for an invalid URL. |
| 34 | */ |
Ray Milkey | 2287d88 | 2015-01-30 10:15:20 -0800 | [diff] [blame] | 35 | @Test |
| 36 | public void badUrl() { |
| 37 | WebResource rs = resource(); |
| 38 | try { |
| 39 | rs.path("ThisIsABadURL").get(String.class); |
| 40 | fail("Fetch of non-existent URL did not throw an exception"); |
| 41 | } catch (UniformInterfaceException ex) { |
| 42 | assertThat(ex.getMessage(), |
| 43 | containsString("returned a response status of 404 Not Found")); |
| 44 | } |
| 45 | } |
Ray Milkey | a0cecdc | 2015-04-09 09:51:52 -0700 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Tests the response for a request with a bad method. |
| 49 | */ |
| 50 | @Test |
| 51 | public void badMethod() { |
| 52 | WebResource rs = resource(); |
| 53 | try { |
| 54 | rs.path("hosts").delete(); |
| 55 | fail("Fetch of non-existent URL did not throw an exception"); |
| 56 | } catch (UniformInterfaceException ex) { |
| 57 | assertThat(ex.getMessage(), |
| 58 | containsString("returned a response status of 405 Method Not Allowed")); |
| 59 | } |
| 60 | } |
Ray Milkey | 2287d88 | 2015-01-30 10:15:20 -0800 | [diff] [blame] | 61 | } |