/** * Parse the given filename and returns the Fixture object. * * @param fileName filename should not contain extension or relative path. ie: login */ public static Fixture parseFrom(String fileName) { return parseFrom(fileName, new YamlParser()); }
/** * Parse the given filename and returns the Fixture object. * * @param fileName filename should not contain extension or relative path. ie: login * @param parser parser is required for parsing operation, it should not be null */ public static Fixture parseFrom(String fileName, Parser parser) { if (fileName == null) { throw new NullPointerException("File name should not be null"); } String path = "fixtures/" + fileName + ".yaml"; InputStream inputStream = openPathAsStream(path); Fixture result = parser.parse(inputStream); if (result.body != null && !result.body.startsWith("{")) { String bodyPath = "fixtures/" + result.body; try { result.body = readPathIntoString(bodyPath); } catch (IOException e) { throw new IllegalStateException("Error reading body: " + bodyPath, e); } } return result; }
/** * Given paths will be parsed to fixtures and added to the queue. Can be multiple */ public List<MockResponse> enqueue(String... paths) { if (paths == null) { return null; } List<MockResponse> mockResponseList = new ArrayList<>(); for (String path : paths) { Fixture fixture = Fixture.parseFrom(path, parser); MockResponse mockResponse = fixture.toMockResponse(); mockWebServer.enqueue(mockResponse); mockResponseList.add(mockResponse); } return mockResponseList; }
private static String readPathIntoString(String path) throws IOException { InputStream inputStream = openPathAsStream(path); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder out = new StringBuilder(); int read; while ((read = reader.read()) != -1) { out.append((char) read); } reader.close(); return out.toString(); }
@Test public void parseWithoutParser() { Fixture fixture = Fixture.parseFrom(Fixtures.SIMPLE_WITH_DELAY); assertThat(fixture.statusCode).isEqualTo(200); assertThat(fixture.delay).isEqualTo(300); assertThat(fixture.body).isEqualTo("{result:{}}"); assertThat(fixture.headers).containsExactly("Auth:auth", "key:value"); }
@Test public void parseFixtureFromYaml() { Fixture fixture = Fixture.parseFrom(Fixtures.SIMPLE_WITH_DELAY, parser); assertThat(fixture.statusCode).isEqualTo(200); assertThat(fixture.delay).isEqualTo(300); assertThat(fixture.body).isEqualTo("{result:{}}"); assertThat(fixture.headers).containsExactly("Auth:auth", "key:value"); }
@Test public void invalidPathThrowsExceptionOnParse() { try { Fixture.parseFrom("invalid_path", parser); fail("should fail"); } catch (Exception e) { assertThat(e).hasMessage("Invalid path: fixtures/invalid_path.yaml"); } try { Fixture.parseFrom(null, parser); fail("should fail"); } catch (Exception e) { assertThat(e).hasMessage("File name should not be null"); } }
@Test public void parseFixtureWithBodyReference() { Fixture fixture = Fixture.parseFrom(Fixtures.SIMPLE_BODY_FILE, parser); assertThat(fixture.statusCode).isEqualTo(200); assertThat(fixture.delay).isEqualTo(0); assertThat(fixture.body).isEqualTo("{\n" + " \"array\": [\n" + " 1,\n" + " 2,\n" + " 3\n" + " ],\n" + " \"boolean\": true,\n" + " \"null\": null,\n" + " \"number\": 123,\n" + " \"object\": {\n" + " \"a\": \"b\",\n" + " \"c\": \"d\",\n" + " \"e\": \"f\"\n" + " },\n" + " \"string\": \"Hello World\"\n" + "}"); assertThat(fixture.headers).containsExactly("Auth:auth", "key:value"); } }