View Javadoc
1   /*
2    * SPDX-License-Identifier: MIT
3    * See LICENSE file for details.
4    *
5    * Copyright 2010-2026 The Waffle Project Contributors: https://github.com/Waffle/waffle/graphs/contributors
6    */
7   package waffle.mock.http;
8   
9   import java.io.IOException;
10  import java.io.PrintWriter;
11  
12  import org.junit.jupiter.api.Assertions;
13  import org.junit.jupiter.api.BeforeEach;
14  import org.junit.jupiter.api.Test;
15  
16  /**
17   * Tests for {@link SimpleHttpResponse}.
18   */
19  class SimpleHttpResponseTest {
20  
21      /** The response. */
22      private SimpleHttpResponse response;
23  
24      /**
25       * Sets the up.
26       */
27      @BeforeEach
28      void setUp() {
29          this.response = new SimpleHttpResponse();
30      }
31  
32      /**
33       * Test initial status is 500.
34       */
35      @Test
36      void testInitialStatus() {
37          Assertions.assertEquals(500, this.response.getStatus());
38      }
39  
40      /**
41       * Test send error with int sets status.
42       */
43      @Test
44      void testSendErrorInt() {
45          this.response.sendError(403);
46          Assertions.assertEquals(403, this.response.getStatus());
47      }
48  
49      /**
50       * Test send error with int and message sets status.
51       */
52      @Test
53      void testSendErrorIntString() {
54          this.response.sendError(404, "Not Found");
55          Assertions.assertEquals(404, this.response.getStatus());
56      }
57  
58      /**
59       * Test set status.
60       */
61      @Test
62      void testSetStatus() {
63          this.response.setStatus(200);
64          Assertions.assertEquals(200, this.response.getStatus());
65      }
66  
67      /**
68       * Test get status string for 401.
69       */
70      @Test
71      void testGetStatusStringUnauthorized() {
72          this.response.setStatus(401);
73          Assertions.assertEquals("Unauthorized", this.response.getStatusString());
74      }
75  
76      /**
77       * Test get status string for unknown status.
78       */
79      @Test
80      void testGetStatusStringUnknown() {
81          this.response.setStatus(200);
82          Assertions.assertEquals("Unknown", this.response.getStatusString());
83      }
84  
85      /**
86       * Test add header and get header values.
87       */
88      @Test
89      void testAddHeaderAndGetHeaderValues() {
90          this.response.addHeader("X-Custom", "value1");
91          this.response.addHeader("X-Custom", "value2");
92          final String[] values = this.response.getHeaderValues("X-Custom");
93          Assertions.assertNotNull(values);
94          Assertions.assertEquals(2, values.length);
95      }
96  
97      /**
98       * Test get header values returns null for missing header.
99       */
100     @Test
101     void testGetHeaderValuesNullForMissing() {
102         Assertions.assertNull(this.response.getHeaderValues("X-Missing"));
103     }
104 
105     /**
106      * Test set header replaces value.
107      */
108     @Test
109     void testSetHeaderReplacesValue() {
110         this.response.addHeader("X-Single", "old");
111         this.response.setHeader("X-Single", "new");
112         Assertions.assertEquals("new", this.response.getHeader("X-Single"));
113     }
114 
115     /**
116      * Test get header returns null for missing.
117      */
118     @Test
119     void testGetHeaderNullForMissing() {
120         Assertions.assertNull(this.response.getHeader("X-NotSet"));
121     }
122 
123     /**
124      * Test get writer.
125      */
126     @Test
127     void testGetWriter() {
128         final PrintWriter writer = this.response.getWriter();
129         Assertions.assertNotNull(writer);
130         writer.print("hello");
131         final String text = this.response.getOutputText();
132         Assertions.assertEquals("hello", text);
133     }
134 
135     /**
136      * Test get output stream.
137      *
138      * @throws IOException
139      *             Signals that an I/O exception has occurred.
140      */
141     @Test
142     void testGetOutputStream() throws IOException {
143         final javax.servlet.ServletOutputStream out = this.response.getOutputStream();
144         Assertions.assertNotNull(out);
145         out.write('A');
146         Assertions.assertTrue(this.response.getOutputText().contains("A"));
147     }
148 
149     /**
150      * Test get output text initially empty.
151      */
152     @Test
153     void testGetOutputTextEmpty() {
154         Assertions.assertEquals("", this.response.getOutputText());
155     }
156 
157     /**
158      * Test flush buffer does not throw.
159      */
160     @Test
161     void testFlushBufferDoesNotThrow() {
162         this.response.sendError(401);
163         Assertions.assertDoesNotThrow(() -> this.response.flushBuffer());
164     }
165 
166     /**
167      * Test header names size.
168      */
169     @Test
170     void testGetHeaderNamesSize() {
171         Assertions.assertEquals(0, this.response.getHeaderNamesSize());
172         this.response.addHeader("X-One", "v1");
173         this.response.addHeader("X-Two", "v2");
174         Assertions.assertEquals(2, this.response.getHeaderNamesSize());
175     }
176 }