-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from mrmike/support-options
Support curl options
- Loading branch information
Showing
9 changed files
with
222 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.moczul.ok2curl; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Set; | ||
|
||
public class Options { | ||
|
||
public static final Options EMPTY = new Options(Collections.<String>emptyList()); | ||
|
||
private final List<String> options; | ||
|
||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
private Options(Collection<String> options) { | ||
this.options = new ArrayList<>(options); | ||
} | ||
|
||
public List<String> list() { | ||
return options; | ||
} | ||
|
||
public static class Builder { | ||
|
||
private final Set<String> options = new HashSet<>(); | ||
|
||
public Builder insecure() { | ||
options.add("--insecure"); | ||
return this; | ||
} | ||
|
||
public Builder maxTime(int seconds) { | ||
options.add(String.format(Locale.getDefault(), "--max-time %d", seconds)); | ||
return this; | ||
} | ||
|
||
public Builder connectTimeout(int seconds) { | ||
options.add(String.format(Locale.getDefault(), "--connect-timeout %d", seconds)); | ||
return this; | ||
} | ||
|
||
public Builder retry(int num) { | ||
options.add(String.format(Locale.getDefault(), "--retry %d", num)); | ||
return this; | ||
} | ||
|
||
public Builder compressed() { | ||
options.add("--compressed"); | ||
return this; | ||
} | ||
|
||
public Builder location() { | ||
options.add("--location"); | ||
return this; | ||
} | ||
|
||
public Options build() { | ||
return new Options(options); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.moczul.ok2curl; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.hasItem; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class OptionsTest { | ||
|
||
@Test | ||
public void defaultBuilderShouldReturnEmptyOptions() { | ||
final Options options = Options.builder().build(); | ||
|
||
assertThat(options.list().size(), is(0)); | ||
} | ||
|
||
@Test | ||
public void shouldCorrectlyHandleOptionsParameters() { | ||
final Options options = Options | ||
.builder() | ||
.maxTime(120) | ||
.connectTimeout(60) | ||
.retry(3) | ||
.build(); | ||
|
||
assertThat(options.list().size(), is(3)); | ||
assertThat(options.list(), hasItem("--max-time 120")); | ||
assertThat(options.list(), hasItem("--connect-timeout 60")); | ||
assertThat(options.list(), hasItem("--retry 3")); | ||
} | ||
|
||
@Test | ||
public void shouldReturnCorrectListOfParameters() { | ||
final Options options = Options.builder() | ||
.insecure() | ||
.compressed() | ||
.location() | ||
.build(); | ||
|
||
assertThat(options.list().size(), is(3)); | ||
assertThat(options.list(), hasItem("--insecure")); | ||
assertThat(options.list(), hasItem("--compressed")); | ||
assertThat(options.list(), hasItem("--location")); | ||
} | ||
|
||
@Test | ||
public void shouldIgnoreDuplicatedParameteres() { | ||
final Options options = Options.builder() | ||
.insecure() | ||
.insecure() | ||
.insecure() | ||
.build(); | ||
|
||
assertThat(options.list().size(), is(1)); | ||
assertThat(options.list(), hasItem("--insecure")); | ||
} | ||
} |
Oops, something went wrong.