File tree Expand file tree Collapse file tree
main/java/com/retailsvc/http/internal
test/java/com/retailsvc/http/internal Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .retailsvc .http .internal ;
2+
3+ import com .retailsvc .http .TypeMapper ;
4+
5+ /**
6+ * Built-in {@link TypeMapper} for {@code application/x-www-form-urlencoded}. Reads delegate to
7+ * {@link FormUrlEncodedParser}. Writes are not supported — form-encoded responses are unusual and
8+ * intentionally left out until a real need surfaces.
9+ */
10+ public final class FormTypeMapper implements TypeMapper {
11+
12+ private final FormUrlEncodedParser parser = new FormUrlEncodedParser ();
13+
14+ @ Override
15+ public Object readFrom (byte [] body , String contentTypeHeader ) {
16+ return parser .parse (body , contentTypeHeader );
17+ }
18+
19+ @ Override
20+ public byte [] writeTo (Object value ) {
21+ throw new UnsupportedOperationException (
22+ "application/x-www-form-urlencoded write is not supported; register a custom TypeMapper" );
23+ }
24+ }
Original file line number Diff line number Diff line change 1+ package com .retailsvc .http .internal ;
2+
3+ import com .retailsvc .http .TypeMapper ;
4+ import java .nio .charset .StandardCharsets ;
5+
6+ /**
7+ * Built-in {@link TypeMapper} for {@code text/plain}. Reads decode bytes using the charset declared
8+ * on {@code Content-Type} (default UTF-8). Writes return {@code String.valueOf(value)} encoded as
9+ * UTF-8.
10+ */
11+ public final class TextTypeMapper implements TypeMapper {
12+
13+ private final TextPlainParser parser = new TextPlainParser ();
14+
15+ @ Override
16+ public Object readFrom (byte [] body , String contentTypeHeader ) {
17+ return parser .parse (body , contentTypeHeader );
18+ }
19+
20+ @ Override
21+ public byte [] writeTo (Object value ) {
22+ return String .valueOf (value ).getBytes (StandardCharsets .UTF_8 );
23+ }
24+ }
Original file line number Diff line number Diff line change 1+ package com .retailsvc .http .internal ;
2+
3+ import static org .assertj .core .api .Assertions .assertThat ;
4+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
5+
6+ import java .nio .charset .StandardCharsets ;
7+ import java .util .Map ;
8+ import org .junit .jupiter .api .Test ;
9+
10+ class FormTypeMapperTest {
11+
12+ private final FormTypeMapper mapper = new FormTypeMapper ();
13+
14+ @ Test
15+ void readsKeyValuePairs () {
16+ byte [] body = "name=Alice&color=blue" .getBytes (StandardCharsets .UTF_8 );
17+ Object parsed = mapper .readFrom (body , "application/x-www-form-urlencoded" );
18+ assertThat (parsed ).isInstanceOf (Map .class );
19+ @ SuppressWarnings ("unchecked" )
20+ Map <String , Object > m = (Map <String , Object >) parsed ;
21+ assertThat (m ).containsEntry ("name" , "Alice" ).containsEntry ("color" , "blue" );
22+ }
23+
24+ @ Test
25+ void writeToIsUnsupported () {
26+ assertThatThrownBy (() -> mapper .writeTo (Map .of ("k" , "v" )))
27+ .isInstanceOf (UnsupportedOperationException .class );
28+ }
29+ }
Original file line number Diff line number Diff line change 1+ package com .retailsvc .http .internal ;
2+
3+ import static org .assertj .core .api .Assertions .assertThat ;
4+
5+ import java .nio .charset .StandardCharsets ;
6+ import org .junit .jupiter .api .Test ;
7+
8+ class TextTypeMapperTest {
9+
10+ private final TextTypeMapper mapper = new TextTypeMapper ();
11+
12+ @ Test
13+ void readsUtf8ByDefault () {
14+ byte [] body = "hello" .getBytes (StandardCharsets .UTF_8 );
15+ assertThat (mapper .readFrom (body , "text/plain" )).isEqualTo ("hello" );
16+ }
17+
18+ @ Test
19+ void readsExplicitCharset () {
20+ byte [] body = "räksmörgås" .getBytes (StandardCharsets .ISO_8859_1 );
21+ assertThat (mapper .readFrom (body , "text/plain; charset=ISO-8859-1" )).isEqualTo ("räksmörgås" );
22+ }
23+
24+ @ Test
25+ void writesStringValueAsUtf8 () {
26+ assertThat (mapper .writeTo ("ok" )).isEqualTo ("ok" .getBytes (StandardCharsets .UTF_8 ));
27+ assertThat (mapper .writeTo (42 )).isEqualTo ("42" .getBytes (StandardCharsets .UTF_8 ));
28+ assertThat (mapper .writeTo (null )).isEqualTo ("null" .getBytes (StandardCharsets .UTF_8 ));
29+ }
30+ }
You can’t perform that action at this time.
0 commit comments