|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.sql.plugin; |
| 9 | + |
| 10 | +import org.elasticsearch.common.xcontent.NamedXContentRegistry; |
| 11 | +import org.elasticsearch.core.TimeValue; |
| 12 | +import org.elasticsearch.rest.RestRequest; |
| 13 | +import org.elasticsearch.test.ESTestCase; |
| 14 | +import org.elasticsearch.test.rest.FakeRestRequest; |
| 15 | +import org.elasticsearch.xpack.sql.action.SqlQueryRequest; |
| 16 | +import org.elasticsearch.xpack.sql.plugin.SqlMediaTypeParser.SqlMediaType; |
| 17 | +import org.elasticsearch.xpack.sql.proto.Mode; |
| 18 | +import org.elasticsearch.xpack.sql.proto.RequestInfo; |
| 19 | + |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import static org.elasticsearch.xpack.sql.plugin.SqlMediaTypeParser.getResponseMediaType; |
| 25 | +import static org.elasticsearch.xpack.sql.plugin.TextFormat.CSV; |
| 26 | +import static org.elasticsearch.xpack.sql.plugin.TextFormat.PLAIN_TEXT; |
| 27 | +import static org.elasticsearch.xpack.sql.plugin.TextFormat.TSV; |
| 28 | +import static org.elasticsearch.xpack.sql.proto.RequestInfo.CLIENT_IDS; |
| 29 | +import static org.hamcrest.CoreMatchers.is; |
| 30 | + |
| 31 | +public class SqlMediaTypeParserTests extends ESTestCase { |
| 32 | + |
| 33 | + public void testPlainTextDetection() { |
| 34 | + SqlMediaType text = getResponseMediaType(reqWithAccept("text/plain"), createTestInstance()); |
| 35 | + assertThat(text.textFormat(), is(PLAIN_TEXT)); |
| 36 | + } |
| 37 | + |
| 38 | + public void testCsvDetection() { |
| 39 | + SqlMediaType text = getResponseMediaType(reqWithAccept("text/csv"), createTestInstance()); |
| 40 | + assertThat(text.textFormat(), is(CSV)); |
| 41 | + |
| 42 | + text = getResponseMediaType(reqWithAccept("text/csv; delimiter=x"), createTestInstance()); |
| 43 | + assertThat(text.textFormat(), is(CSV)); |
| 44 | + } |
| 45 | + |
| 46 | + public void testTsvDetection() { |
| 47 | + SqlMediaType text = getResponseMediaType(reqWithAccept("text/tab-separated-values"), |
| 48 | + createTestInstance()); |
| 49 | + assertThat(text.textFormat(), is(TSV)); |
| 50 | + } |
| 51 | + |
| 52 | + public void testMediaTypeDetectionWithParameters() { |
| 53 | + assertThat(getResponseMediaType(reqWithAccept("text/plain; charset=utf-8"), |
| 54 | + createTestInstance()).textFormat(), is(PLAIN_TEXT)); |
| 55 | + assertThat(getResponseMediaType(reqWithAccept("text/plain; header=present"), |
| 56 | + createTestInstance()).textFormat(), is(PLAIN_TEXT)); |
| 57 | + assertThat(getResponseMediaType(reqWithAccept("text/plain; charset=utf-8; header=present"), |
| 58 | + createTestInstance()).textFormat(), is(PLAIN_TEXT)); |
| 59 | + |
| 60 | + assertThat(getResponseMediaType(reqWithAccept("text/csv; charset=utf-8"), |
| 61 | + createTestInstance()).textFormat(), is(CSV)); |
| 62 | + assertThat(getResponseMediaType(reqWithAccept("text/csv; header=present"), |
| 63 | + createTestInstance()).textFormat(), is(CSV)); |
| 64 | + assertThat(getResponseMediaType(reqWithAccept("text/csv; charset=utf-8; header=present"), |
| 65 | + createTestInstance()).textFormat(), is(CSV)); |
| 66 | + |
| 67 | + assertThat(getResponseMediaType(reqWithAccept("text/tab-separated-values; charset=utf-8"), |
| 68 | + createTestInstance()).textFormat(), is(TSV)); |
| 69 | + assertThat(getResponseMediaType(reqWithAccept("text/tab-separated-values; header=present"), |
| 70 | + createTestInstance()).textFormat(), is(TSV)); |
| 71 | + assertThat(getResponseMediaType(reqWithAccept("text/tab-separated-values; charset=utf-8; header=present"), |
| 72 | + createTestInstance()).textFormat(), is(TSV)); |
| 73 | + } |
| 74 | + |
| 75 | + public void testInvalidFormat() { |
| 76 | + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, |
| 77 | + () -> getResponseMediaType(reqWithAccept("text/garbage"), createTestInstance())); |
| 78 | + assertEquals(e.getMessage(), "invalid format [text/garbage]"); |
| 79 | + } |
| 80 | + |
| 81 | + public void testNoFormat() { |
| 82 | + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, |
| 83 | + () -> getResponseMediaType(new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).build(), |
| 84 | + createTestInstance())); |
| 85 | + assertEquals(e.getMessage(), "Invalid request content type: Accept=[null], Content-Type=[null], format=[null]"); |
| 86 | + } |
| 87 | + |
| 88 | + private static RestRequest reqWithAccept(String acceptHeader) { |
| 89 | + |
| 90 | + return new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY) |
| 91 | + .withHeaders(new HashMap<String, List<String>>() {{ |
| 92 | + put("Content-Type", Collections.singletonList("application/json")); |
| 93 | + put("Accept", Collections.singletonList(acceptHeader)); |
| 94 | + }}).build(); |
| 95 | + } |
| 96 | + |
| 97 | + protected SqlQueryRequest createTestInstance() { |
| 98 | + return new SqlQueryRequest(randomAlphaOfLength(10), Collections.emptyList(), null, null, |
| 99 | + randomZone(), between(1, Integer.MAX_VALUE), TimeValue.parseTimeValue(randomTimeValue(), null, "test"), |
| 100 | + TimeValue.parseTimeValue(randomTimeValue(), null, "test"), false, randomAlphaOfLength(10), |
| 101 | + new RequestInfo(Mode.PLAIN, randomFrom(randomFrom(CLIENT_IDS), randomAlphaOfLengthBetween(10, 20))), |
| 102 | + randomBoolean(), randomBoolean()).binaryCommunication(false); |
| 103 | + } |
| 104 | +} |
0 commit comments