Skip to content

Commit 1752ada

Browse files
committed
added mores tests
1 parent 0fd826f commit 1752ada

File tree

288 files changed

+3568
-17
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

288 files changed

+3568
-17
lines changed

httpsnippet/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@
8686
<version>${slf4j.version}</version>
8787
<scope>test</scope>
8888
</dependency>
89+
<dependency>
90+
<groupId>commons-io</groupId>
91+
<artifactId>commons-io</artifactId>
92+
<version>${commons-io.version}</version>
93+
<scope>test</scope>
94+
</dependency>
8995
</dependencies>
9096

9197
<build>

httpsnippet/src/main/java/io/github/atkawa7/httpsnippet/models/internal/CodeRequest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ private void validateMimeType() throws Exception {
110110
if (!this._hasParams) {
111111
throw new Exception("Params cannot be empty");
112112
}
113-
if (!this._hasAttachments) {
114-
throw new Exception("Params must have attachments");
115-
}
113+
// if (!this._hasAttachments) {
114+
// throw new Exception("Params must have attachments");
115+
// }
116116
}
117117
}
118118

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.github.atkawa7.httpsnippet.fixtures;
2+
3+
import com.smartbear.har.model.HarRequest;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Getter;
6+
7+
@AllArgsConstructor
8+
@Getter
9+
public class Fixture {
10+
private FixtureType fixtureType;
11+
private HarRequest harRequest;
12+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package io.github.atkawa7.httpsnippet.fixtures;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.smartbear.har.model.HarRequest;
5+
import lombok.AccessLevel;
6+
import lombok.NoArgsConstructor;
7+
import lombok.extern.slf4j.Slf4j;
8+
9+
import java.nio.file.Path;
10+
import java.nio.file.Paths;
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
14+
@Slf4j
15+
@NoArgsConstructor(access = AccessLevel.PRIVATE)
16+
public class FixtureBuilder {
17+
private static final ObjectMapper objectMapper = new ObjectMapper();
18+
private List<FixtureType> fixtureTypes = new ArrayList<>();
19+
public static FixtureBuilder builder(){
20+
return new FixtureBuilder();
21+
}
22+
23+
public FixtureBuilder applicationFormEncoded(){
24+
this.fixtureTypes.add(FixtureType.APPLICATION_FORM_ENCODED);
25+
return this;
26+
}
27+
28+
public FixtureBuilder applicationJson(){
29+
this.fixtureTypes.add(FixtureType.APPLICATION_JSON);
30+
return this;
31+
}
32+
33+
public FixtureBuilder cookies(){
34+
this.fixtureTypes.add(FixtureType.COOKIES);
35+
return this;
36+
}
37+
38+
public FixtureBuilder customMethod(){
39+
this.fixtureTypes.add(FixtureType.CUSTOM_METHOD);
40+
return this;
41+
}
42+
43+
public FixtureBuilder fullRequest(){
44+
this.fixtureTypes.add(FixtureType.FULL_REQUEST);
45+
return this;
46+
}
47+
48+
49+
public FixtureBuilder headers(){
50+
this.fixtureTypes.add(FixtureType.HEADERS);
51+
return this;
52+
}
53+
54+
public FixtureBuilder https(){
55+
this.fixtureTypes.add(FixtureType.HTTPS);
56+
return this;
57+
}
58+
59+
public FixtureBuilder jsonObjectNull(){
60+
this.fixtureTypes.add(FixtureType.JSON_OBJECT_NULL);
61+
return this;
62+
}
63+
64+
public FixtureBuilder multipartData(){
65+
this.fixtureTypes.add(FixtureType.MULTIPART_DATA);
66+
return this;
67+
}
68+
69+
public FixtureBuilder multipartFile(){
70+
this.fixtureTypes.add(FixtureType.MULTIPART_FILE);
71+
return this;
72+
}
73+
public FixtureBuilder multipartFormData(){
74+
this.fixtureTypes.add(FixtureType.MULTIPART_FORM_DATA);
75+
return this;
76+
}
77+
78+
public FixtureBuilder query(){
79+
this.fixtureTypes.add(FixtureType.QUERY);
80+
return this;
81+
}
82+
83+
public FixtureBuilder shortRequest(){
84+
this.fixtureTypes.add(FixtureType.SHORT_REQUEST);
85+
return this;
86+
}
87+
88+
public FixtureBuilder textPlain(){
89+
this.fixtureTypes.add(FixtureType.TEXT_PLAIN);
90+
return this;
91+
}
92+
93+
public List<Fixture> build() throws Exception{
94+
List<Fixture> fixtures =new ArrayList<>();
95+
Path currentPath = Paths.get(this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath());
96+
Path fixturePath = Paths.get(currentPath.toString(), "fixtures");
97+
for(FixtureType fixtureType: fixtureTypes){
98+
Path filePath = Paths.get(fixturePath.toString(), String.format("%s.json", fixtureType.getName()));
99+
HarRequest harRequest = objectMapper.readValue(filePath.toFile(), HarRequest.class);
100+
fixtures.add(new Fixture(fixtureType, harRequest));
101+
}
102+
return fixtures;
103+
}
104+
105+
106+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package io.github.atkawa7.httpsnippet.fixtures;
2+
3+
import io.github.atkawa7.httpsnippet.generators.HttpSnippetCodeGenerator;
4+
import io.github.atkawa7.httpsnippet.models.Client;
5+
import io.github.atkawa7.httpsnippet.models.HttpSnippet;
6+
import io.github.atkawa7.httpsnippet.models.Language;
7+
import lombok.extern.slf4j.Slf4j;
8+
import org.apache.commons.io.FileUtils;
9+
import org.apache.commons.io.FilenameUtils;
10+
import org.junit.jupiter.api.Test;
11+
import org.junit.jupiter.params.ParameterizedTest;
12+
import org.junit.jupiter.params.provider.MethodSource;
13+
14+
import java.io.File;
15+
import java.net.FileNameMap;
16+
import java.nio.file.Path;
17+
import java.nio.file.Paths;
18+
import java.util.List;
19+
import java.util.stream.Stream;
20+
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
23+
@Slf4j
24+
public class FixtureTest {
25+
26+
public static Stream<Fixture> fixtureStream() throws Exception{
27+
List<Fixture> fixtures = FixtureBuilder.builder()
28+
.applicationFormEncoded()
29+
.applicationJson()
30+
.cookies()
31+
.customMethod()
32+
.fullRequest()
33+
.shortRequest()
34+
.headers()
35+
.multipartData()
36+
.multipartFile()
37+
.multipartFormData()
38+
.query()
39+
.textPlain()
40+
.jsonObjectNull()
41+
.https()
42+
.build();
43+
return fixtures.stream();
44+
}
45+
46+
@ParameterizedTest
47+
@MethodSource("fixtureStream")
48+
void testFixtures(Fixture fixture) throws Exception{
49+
Path currentPath = Paths.get(this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath());
50+
Path output = Paths.get(currentPath.toString(), "output");
51+
52+
HttpSnippetCodeGenerator codeGenerator = new HttpSnippetCodeGenerator();
53+
List<HttpSnippet> snippets = codeGenerator.snippets(fixture.getHarRequest());
54+
for(HttpSnippet snippet: snippets){
55+
56+
logger.info("{}");
57+
Client client = snippet.getClient();
58+
Language language = snippet.getLanguage();
59+
String code = snippet.getCode();
60+
Path codeDir = Paths.get(output.toString(),
61+
language.getKey().toLowerCase().replaceAll("[^a-zA-Z0-9-_\\.]", ""),
62+
client.getKey().toLowerCase().replaceAll("[^a-zA-Z0-9-_\\.]", ""));
63+
Path codePath =Paths.get(codeDir.toString(), String.format("%s%s", fixture.getFixtureType().getName(), language.getExtname()));
64+
65+
assertEquals(FileUtils.readFileToString(codePath.toFile()), code);
66+
}
67+
68+
69+
}
70+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package io.github.atkawa7.httpsnippet.fixtures;
2+
3+
public enum FixtureType {
4+
MULTIPART_FILE("multipart-file"),
5+
APPLICATION_JSON("application-json"),
6+
QUERY("query"),
7+
FULL_REQUEST("full"),
8+
HEADERS("headers"),
9+
MULTIPART_FORM_DATA("multipart-form-data"),
10+
HTTPS("https"),
11+
COOKIES("cookies"),
12+
SHORT_REQUEST("short"),
13+
CUSTOM_METHOD("custom-method"),
14+
APPLICATION_FORM_ENCODED("application-form-encoded"),
15+
MULTIPART_DATA("multipart-data"),
16+
TEXT_PLAIN("text-plain"),
17+
JSON_OBJECT_NULL("jsonObj-null-value");
18+
private String name;
19+
20+
FixtureType(String name) {
21+
this.name = name;
22+
}
23+
24+
public String getName() {
25+
return name;
26+
}
27+
}

httpsnippet/src/test/java/io/github/atkawa7/httpsnippet/models/internal/CodeRequestTest.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -128,20 +128,6 @@ void testExceptionIsThrownWhenContentTypeIsMultiFormDataAndListOfParamsIsEmpty()
128128
assertEquals("Params cannot be empty", thrown.getMessage());
129129
}
130130

131-
@Test
132-
void testExceptionIsRaisedWhenContentTypeIsMultiFormDataAndParamsDoesNotHaveAttachment() {
133-
List<HarParam> harParams = new ArrayList<>();
134-
harParams.add(new HarParam("foo", "bar", null, null, null));
135-
HarRequest harRequest =
136-
new HarRequestBuilder()
137-
.withUrl(HTTP_URL)
138-
.withPostData(new HarPostData(MediaType.MULTIPART_FORM_DATA, harParams, null, null))
139-
.build();
140-
Exception thrown =
141-
assertThrows(
142-
Exception.class, () -> newCodeRequest(harRequest), "Expected exception thrown");
143-
assertEquals("Params must have attachments", thrown.getMessage());
144-
}
145131

146132
@Test
147133
void testExceptionIsRaisedWhenContentTypeIsFormUrlEncodedAndListOfParamsIsEmpty() {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"method": "POST",
3+
"url": "http://mockbin.com/har",
4+
"headers": [
5+
{
6+
"name": "content-type",
7+
"value": "application/x-www-form-urlencoded"
8+
}
9+
],
10+
"postData": {
11+
"mimeType": "application/x-www-form-urlencoded",
12+
"params": [
13+
{
14+
"name": "foo",
15+
"value": "bar"
16+
},
17+
{
18+
"name": "hello",
19+
"value": "world"
20+
}
21+
]
22+
}
23+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"method": "POST",
3+
"url": "http://mockbin.com/har",
4+
"headers": [
5+
{
6+
"name": "content-type",
7+
"value": "application/json"
8+
}
9+
],
10+
"postData": {
11+
"mimeType": "application/json",
12+
"text": "{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}],\"boolean\":false}"
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"method": "POST",
3+
"url": "http://mockbin.com/har",
4+
"cookies": [
5+
{
6+
"name": "foo",
7+
"value": "bar"
8+
},
9+
{
10+
"name": "bar",
11+
"value": "baz"
12+
}
13+
]
14+
}

0 commit comments

Comments
 (0)