|
| 1 | +/* |
| 2 | + * Copyright 2019 Google LLC |
| 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 | + |
| 17 | +package com.example.appengine.cloudrunpubsub; |
| 18 | + |
| 19 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 20 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 21 | + |
| 22 | +import org.junit.Test; |
| 23 | +import org.junit.runner.RunWith; |
| 24 | +import org.springframework.beans.factory.annotation.Autowired; |
| 25 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 26 | +import org.springframework.boot.test.context.SpringBootTest; |
| 27 | +import org.springframework.http.MediaType; |
| 28 | +import org.springframework.test.context.junit4.SpringRunner; |
| 29 | +import org.springframework.test.web.servlet.MockMvc; |
| 30 | + |
| 31 | +@RunWith(SpringRunner.class) |
| 32 | +@SpringBootTest |
| 33 | +@AutoConfigureMockMvc |
| 34 | +public class PubsubControllerTests { |
| 35 | + |
| 36 | + @Autowired private MockMvc mockMvc; |
| 37 | + |
| 38 | + @Test |
| 39 | + public void addEmptyBody() throws Exception { |
| 40 | + mockMvc.perform(post("/")).andExpect(status().isBadRequest()); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void addNoMessage() throws Exception { |
| 45 | + String mockBody = "{}"; |
| 46 | + |
| 47 | + mockMvc |
| 48 | + .perform(post("/").contentType(MediaType.APPLICATION_JSON).content(mockBody)) |
| 49 | + .andExpect(status().isBadRequest()); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void addInvalidMimetype() throws Exception { |
| 54 | + String mockBody = "{\"message\":{\"data\":\"dGVzdA==\"," |
| 55 | + + "\"attributes\":{},\"messageId\":\"91010751788941\"" |
| 56 | + + ",\"publishTime\":\"2017-09-25T23:16:42.302Z\"}}"; |
| 57 | + |
| 58 | + mockMvc |
| 59 | + .perform(post("/").contentType(MediaType.TEXT_HTML).content(mockBody)) |
| 60 | + .andExpect(status().isUnsupportedMediaType()); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void addMinimalBody() throws Exception { |
| 65 | + String mockBody = "{\"message\":{}}"; |
| 66 | + |
| 67 | + mockMvc |
| 68 | + .perform(post("/").contentType(MediaType.APPLICATION_JSON).content(mockBody)) |
| 69 | + .andExpect(status().isOk()); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void addFullBody() throws Exception { |
| 74 | + String mockBody = "{\"message\":{\"data\":\"dGVzdA==\"," |
| 75 | + + "\"attributes\":{},\"messageId\":\"91010751788941\"" |
| 76 | + + ",\"publishTime\":\"2017-09-25T23:16:42.302Z\"}}"; |
| 77 | + mockMvc |
| 78 | + .perform(post("/").contentType(MediaType.APPLICATION_JSON).content(mockBody)) |
| 79 | + .andExpect(status().isOk()); |
| 80 | + } |
| 81 | +} |
0 commit comments