├── .github
└── ISSUE_TEMPLATE
│ ├── bug_report.md
│ ├── custom.md
│ └── feature_request.md
├── .gitignore
├── .travis.yml
├── CHANGELOG.md
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── UPGRADE_GUIDE.md
├── build
├── checkstyle.xml
├── fileHeader.txt
└── suppressions.xml
├── deploy
├── pom.xml
└── unirest
├── pom.xml
└── src
├── main
└── java
│ └── unirest
│ ├── ApacheAsyncClient.java
│ ├── ApacheClient.java
│ ├── ApacheRequestWithBody.java
│ ├── ApacheResponse.java
│ ├── AsyncClient.java
│ ├── AsyncIdleConnectionMonitorThread.java
│ ├── BaseApacheClient.java
│ ├── BaseRequest.java
│ ├── BaseResponse.java
│ ├── BasicResponse.java
│ ├── BinaryResponse.java
│ ├── Body.java
│ ├── Callback.java
│ ├── CallbackFuture.java
│ ├── Client.java
│ ├── Config.java
│ ├── Empty.java
│ ├── EmptyResponse.java
│ ├── FileResponse.java
│ ├── FormPart.java
│ ├── GenericType.java
│ ├── GetRequest.java
│ ├── Header.java
│ ├── HeaderNames.java
│ ├── Headers.java
│ ├── HttpDeleteWithBody.java
│ ├── HttpMethod.java
│ ├── HttpPatchWithBody.java
│ ├── HttpRequest.java
│ ├── HttpRequestBody.java
│ ├── HttpRequestJsonPatch.java
│ ├── HttpRequestMultiPart.java
│ ├── HttpRequestNoBody.java
│ ├── HttpRequestUniBody.java
│ ├── HttpRequestWithBody.java
│ ├── HttpResponse.java
│ ├── JsonNode.java
│ ├── JsonPatch.java
│ ├── JsonPatchItem.java
│ ├── JsonPatchOperation.java
│ ├── JsonPatchRequest.java
│ ├── JsonResponse.java
│ ├── MultipartBody.java
│ ├── NoRedirects.java
│ ├── ObjectMapper.java
│ ├── ObjectResponse.java
│ ├── PagedList.java
│ ├── Path.java
│ ├── RawResponse.java
│ ├── RawResponseBase.java
│ ├── RequestBodyEntity.java
│ ├── RequestPrep.java
│ ├── StringResponse.java
│ ├── SyncIdleConnectionMonitorThread.java
│ ├── Unirest.java
│ ├── UnirestConfigException.java
│ ├── UnirestException.java
│ ├── UnirestInstance.java
│ ├── UnirestParsingException.java
│ └── Util.java
└── test
├── java
├── BehaviorTests
│ ├── AsBinaryTest.java
│ ├── AsEmptyTest.java
│ ├── AsFileTests.java
│ ├── AsGenericTypeTest.java
│ ├── AsJsonTest.java
│ ├── AsObjectTest.java
│ ├── AsStringTest.java
│ ├── BddTest.java
│ ├── CallbackFutureTest.java
│ ├── ConsumerTests.java
│ ├── DefectTest.java
│ ├── Foo.java
│ ├── FormPostingTest.java
│ ├── GZipTest.java
│ ├── GetResponse.java
│ ├── HeaderTest.java
│ ├── InterceptorTest.java
│ ├── JankyProxy.java
│ ├── JsonPatchTest.java
│ ├── LifeCycleTest.java
│ ├── MockServer.java
│ ├── ObjectFunctionalTest.java
│ ├── PagingTest.java
│ ├── Pair.java
│ ├── PathParamTest.java
│ ├── PostRequestHandlersTest.java
│ ├── ProxyTest.java
│ ├── QueryStringTest.java
│ ├── RedirectHandling.java
│ ├── RequestCapture.java
│ ├── ResponseHeaderTest.java
│ ├── SendBodyTest.java
│ ├── TimeoutTests.java
│ └── VerbTest.java
└── unirest
│ ├── ClientFactoryTest.java
│ ├── ConfigTest.java
│ ├── GsonObjectMapper.java
│ ├── HeadersTest.java
│ ├── JacksonObjectMapper.java
│ ├── JacksonObjectMapperTest.java
│ ├── JsonNodeTest.java
│ ├── MockCallback.java
│ ├── NoopCallback.java
│ ├── NotImplimented.java
│ ├── PagedListTest.java
│ ├── ResponseUtilsTest.java
│ ├── TestUtil.java
│ ├── UriFormatterTest.java
│ └── UtilTest.java
└── resources
├── data
└── cp1250.txt
├── image.jpg
├── test
└── test-json-patch.json
/.github/ISSUE_TEMPLATE/bug_report.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Bug report
3 | about: Create a report to help us improve
4 | title: ''
5 | labels: ''
6 | assignees: ''
7 |
8 | ---
9 |
10 | **Describe the bug**
11 | A clear and concise description of what the bug is.
12 |
13 | **To Reproduce**
14 | Steps to reproduce the behavior:
15 | 1. Provide as much of a code sample as possible. Unirest is pretty small so it you should be able to mostly copy-paste into the issue
16 | 1. do **NOT** paste authentication or client keys into the issue
17 | 1. provide full stack traces if possible
18 |
19 | **Expected behavior**
20 | A clear and concise description of what you expected to happen.
21 |
22 | **Screenshots**
23 | If applicable, add screenshots to help explain your problem.
24 |
25 | **Environmental Data:**
26 | - Java Version
27 | - Version [e.g. 3.3.03]
28 |
29 | **Additional context**
30 | Add any other context about the problem here.
31 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/custom.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Custom issue template
3 | about: Describe this issue template's purpose here.
4 | title: ''
5 | labels: ''
6 | assignees: ''
7 |
8 | ---
9 |
10 |
11 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/feature_request.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Feature request
3 | about: Suggest an idea for this project
4 | title: ''
5 | labels: ''
6 | assignees: ''
7 |
8 | ---
9 |
10 | **Is your feature request related to a problem? Please describe.**
11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12 |
13 | **Describe the solution you'd like**
14 | A clear and concise description of what you want to happen.
15 |
16 | **Describe alternatives you've considered**
17 | A clear and concise description of any alternative solutions or features you've considered.
18 |
19 | **Additional context**
20 | Add any other context or screenshots about the feature request here.
21 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .classpath
2 | pom.xml.releaseBackup
3 | .project
4 | target
5 | .DS_Store
6 | .settings
7 | release.properties
8 | META-INF
9 | .idea
10 | *.iml
11 | .gitconfig
12 |
13 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: java
2 |
3 | jdk:
4 | - oraclejdk8
5 |
6 | os:
7 | - linux
8 |
9 | cache:
10 | directories:
11 | - .autoconf
12 | - $HOME/.m2
13 |
14 | # blocklist
15 | branches:
16 | except:
17 | - v3
18 |
--------------------------------------------------------------------------------
/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
1 | # Contributor Covenant Code of Conduct
2 |
3 | ## Our Pledge
4 |
5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
6 |
7 | ## Our Standards
8 |
9 | Examples of behavior that contributes to creating a positive environment include:
10 |
11 | * Using welcoming and inclusive language
12 | * Being respectful of differing viewpoints and experiences
13 | * Gracefully accepting constructive criticism
14 | * Focusing on what is best for the community
15 | * Showing empathy towards other community members
16 |
17 | Examples of unacceptable behavior by participants include:
18 |
19 | * The use of sexualized language or imagery and unwelcome sexual attention or advances
20 | * Trolling, insulting/derogatory comments, and personal or political attacks
21 | * Public or private harassment
22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission
23 | * Other conduct which could reasonably be considered inappropriate in a professional setting
24 |
25 | ## Our Responsibilities
26 |
27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
28 |
29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
30 |
31 | ## Scope
32 |
33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
34 |
35 | ## Enforcement
36 |
37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at ryan.bergman@gmail.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
38 |
39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
40 |
41 | ## Attribution
42 |
43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
44 |
45 | [homepage]: http://contributor-covenant.org
46 | [version]: http://contributor-covenant.org/version/1/4/
47 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # Contributing to Open Unirest
2 |
3 | :+1::tada: First off, thanks for taking the time to contribute! :tada::+1:
4 |
5 | ## Code of Conduct
6 |
7 | This project and everyone participating in it is governed by the [Open Unirest Code of Conduct](CODE_OF_CONDUCT.md). By participating, you are expected to uphold this code.
8 |
9 | ## How Can I Contribute?
10 |
11 | ### Reporting Bugs
12 | **Explain how to produce the problem** in as many details as possible. When listing steps, **don't just say what you did, but explain how you did it**.
13 | * **Provide specific examples to demonstrate the steps**. Include links to files or GitHub projects, or copy/pasteable snippets, which you use in those examples. If you're providing snippets in the issue, use [Markdown code blocks](https://help.github.com/articles/markdown-basics/#multiple-lines).
14 | * **Describe the behavior you observed after following the steps** and point out what exactly is the problem with that behavior.
15 | * **Explain which behavior you expected to see instead and why.**
16 |
17 | Include details about your configuration and environment:
18 |
19 | * **What version of Unirest were you using?**
20 | * **What version of Java were you running it on?**
21 | * **Did you do any custom configuration to the client?**
22 |
23 |
24 | ### Contributing Code
25 |
26 | * Before you put in all the work to add a new feature. Try opening a issue and checking if it's something that will be accepted. Sometimes a feature you think might be useful might be contrary to the goals or direction of the project. We won't want folks to get frustrated, so take a minute and talk it through with us.
27 | * If we do all agree it's a good idea we would expect:
28 | * Backwards compatibility is important to this project and you should go out of your way to not make breaking changes.
29 | * All contributions should be issued as pull requests and get reviewed by the admins of this project.
30 | * All code should follow style guidelines enforced by the checkstyle and other static analysis.
31 | * All code should have tests.
32 | * Unit tests
33 | * Behavioral tests (see https://github.com/OpenUnirest/unirest-java/tree/master/src/test/java/BehaviorTests)
34 | * You should have run verify before submitting and the PR needs to have passed TravisCI before being merged.
35 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License
2 |
3 | Copyright for portions of OpenUnirest/uniresr-java are held by Mashape (c) 2013 as part of Kong/unirest-java.
4 | All other copyright for OpenUnirest/unirest-java are held by OpenUnirest (c) 2018.
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining
7 | a copy of this software and associated documentation files (the
8 | "Software"), to deal in the Software without restriction, including
9 | without limitation the rights to use, copy, modify, merge, publish,
10 | distribute, sublicense, and/or sell copies of the Software, and to
11 | permit persons to whom the Software is furnished to do so, subject to
12 | the following conditions:
13 |
14 | The above copyright notice and this permission notice shall be
15 | included in all copies or substantial portions of the Software.
16 |
17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # This project went home to the mothership
2 |
3 | ### This project has been merged with https://github.com/Kong/unirest-java. Please see that project as the primary place for new features and security issues.
4 |
5 |
--------------------------------------------------------------------------------
/UPGRADE_GUIDE.md:
--------------------------------------------------------------------------------
1 | ## Upgrading to Open Unirest 3.0 from previous versions
2 |
3 | ### Package
4 | Inspired by the "Java Spark" project, all classes are now in the ```unirest``` package. This project only has 45 files, it really doesn't need anything more complicated than that.
5 |
6 | ### Configuration
7 | Previous versions of unirest had configuration split across several different places. Sometimes it was done on ```Unirest```, sometimes it was done on ```Option```, sometimes it was somewhere else.
8 | All configuration is now done through ```Unirest.config()```
9 |
10 | #### Unirest.config()
11 | Unirest config allows easy access to build a configuration just like you would build a request:
12 |
13 | ```java
14 | Unirest.config()
15 | .socketTimeout(500)
16 | .connectTimeout(1000)
17 | .concurrency(10, 5)
18 | .proxy(new HttpHost("https://proxy"))
19 | .setDefaultHeader("Accept", "application/json")
20 | .followRedirects(false)
21 | .enableCookieManagement(false)
22 | .addInterceptor(new MyCustomInterceptor());
23 | ```
24 |
25 | ##### Changing the config
26 | Changing Unirest's config should ideally be done once, or rarely. There are several background threads spawned by both Unirest itself and Apache HttpAsyncClient. Once Unirest has been activated configuration options that are involved in creating the client cannot be changed without an explicit shutdown or reset.
27 |
28 | ```Java
29 | Unirest.config()
30 | .reset()
31 | .connectTimeout(5000)
32 | ```
33 |
34 | ##### Setting custom Apache Client
35 | You can set your own custom Apache HttpClient and HttpAsyncClient. Note that Unirest settings like timeouts or interceptors are not applied to custom clients.
36 |
37 | ```java
38 | Unirest.config()
39 | .httpClient(myClient)
40 | .asyncClient(myAsyncClient)
41 | ```
42 |
43 | #### Multiple Configuration Instances
44 | As usual, Unirest maintains a primary single instance. Sometimes you might want different configurations for different systems. You might also want an instance rather than a static context for testing purposes.
45 |
46 | ```java
47 |
48 | // this returns the same instance used by Unirest.get("http://somewhere/")
49 | UnirestInstance unirest = Unirest.primaryInstance();
50 | // It can be configured and used just like the static context
51 | unirest.config().connectTimeout(5000);
52 | String result = unirest.get("http://foo").asString().getBody();
53 |
54 | // You can also get a whole new instance
55 | UnirestInstance unirest = Unirest.spawnInstance();
56 | ```
57 |
58 | **WARNING!** If you get a new instance of unirest YOU are responsible for shutting it down when the JVM shuts down. It is not tracked or shut down by ```Unirest.shutDown();```
59 |
60 |
61 |
62 |
--------------------------------------------------------------------------------
/build/fileHeader.txt:
--------------------------------------------------------------------------------
1 | The MIT License
2 |
3 | Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining
6 | a copy of this software and associated documentation files (the
7 | "Software"), to deal in the Software without restriction, including
8 | without limitation the rights to use, copy, modify, merge, publish,
9 | distribute, sublicense, and/or sell copies of the Software, and to
10 | permit persons to whom the Software is furnished to do so, subject to
11 | the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be
14 | included in all copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 |
--------------------------------------------------------------------------------
/build/suppressions.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/deploy:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | mvn clean verify
4 | mvn release:prepare -DskipTests
5 | mvn release:perform -DskipTests
6 |
--------------------------------------------------------------------------------
/unirest/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 |
5 | io.github.openunirest
6 | unirest-java-parent
7 | 3.3.07-SNAPSHOT
8 |
9 |
10 | open-unirest-java
11 | jar
12 |
13 |
14 |
15 | org.apache.httpcomponents
16 | httpclient
17 | 4.5.6
18 |
19 |
20 | org.apache.httpcomponents
21 | httpmime
22 | 4.5.6
23 |
24 |
25 | org.apache.httpcomponents
26 | httpasyncclient
27 | 4.1.4
28 |
29 |
30 | org.json
31 | json
32 | 20180813
33 |
34 |
35 | junit
36 | junit
37 | 4.12
38 | test
39 |
40 |
41 |
42 |
43 |
44 | org.hamcrest
45 | hamcrest-all
46 | 1.3
47 | test
48 |
49 |
50 | commons-io
51 | commons-io
52 | 2.4
53 | test
54 |
55 |
56 |
57 | org.mockito
58 | mockito-all
59 | 2.0.2-beta
60 | test
61 |
62 |
63 |
64 | com.google.guava
65 | guava
66 | 27.0.1-jre
67 | test
68 |
69 |
70 |
71 | com.fasterxml.jackson.datatype
72 | jackson-datatype-guava
73 | ${jackson.version}
74 | test
75 |
76 |
77 | com.fasterxml.jackson.core
78 | jackson-databind
79 | ${jackson.version}
80 | test
81 |
82 |
83 | com.sparkjava
84 | spark-core
85 | 2.8.0
86 | test
87 |
88 |
89 | org.skyscreamer
90 | jsonassert
91 | 1.5.0
92 | test
93 |
94 |
95 | org.slf4j
96 | slf4j-simple
97 | 1.7.25
98 | test
99 |
100 |
101 | com.google.code.gson
102 | gson
103 | 2.8.1
104 | test
105 |
106 |
107 | com.github.paweladamski
108 | HttpClientMock
109 | 1.0.4
110 | test
111 |
112 |
113 |
114 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/ApacheRequestWithBody.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
29 |
30 | import java.net.URI;
31 |
32 | public class ApacheRequestWithBody extends HttpEntityEnclosingRequestBase {
33 | private HttpMethod method;
34 |
35 | public ApacheRequestWithBody(HttpMethod method, String uri){
36 | this.method = method;
37 | setURI(URI.create(uri));
38 | }
39 | @Override
40 | public String getMethod() {
41 | return method.name();
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/AsyncClient.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import org.apache.http.nio.client.HttpAsyncClient;
29 |
30 | import java.util.concurrent.CompletableFuture;
31 | import java.util.function.Function;
32 | import java.util.stream.Stream;
33 |
34 | public interface AsyncClient {
35 | /**
36 | * @return the apache HttpAsyncClient.
37 | * @deprecated Eventually the apache implementation will be hidden and other implementation wi be available
38 | */
39 | @Deprecated
40 | HttpAsyncClient getClient();
41 |
42 | /**
43 | * Make a Async request
44 | * @param The type of the body
45 | * @param request the prepared request object
46 | * @param transformer the function to transform the response
47 | * @param callback the CompletableFuture that will handle the eventual response
48 | * @return a CompletableFuture of a response
49 | */
50 | CompletableFuture> request(HttpRequest request, Function> transformer, CompletableFuture> callback);
51 |
52 | /**
53 | * @return a stream of exceptions possibly thrown while closing all the things.
54 | */
55 | default Stream close() {
56 | return Stream.empty();
57 | }
58 |
59 | /**
60 | * @return is the client running?
61 | */
62 | default boolean isRunning() {
63 | return true;
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/AsyncIdleConnectionMonitorThread.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import java.util.concurrent.TimeUnit;
29 |
30 | import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
31 |
32 | public class AsyncIdleConnectionMonitorThread extends Thread {
33 |
34 | private final PoolingNHttpClientConnectionManager connMgr;
35 |
36 | public AsyncIdleConnectionMonitorThread(PoolingNHttpClientConnectionManager connMgr) {
37 | super();
38 | super.setDaemon(true);
39 | this.connMgr = connMgr;
40 | }
41 |
42 | @Override
43 | public void run() {
44 | try {
45 | while (!Thread.currentThread().isInterrupted()) {
46 | synchronized (this) {
47 | wait(5000);
48 | // Close expired connections
49 | connMgr.closeExpiredConnections();
50 | // Optionally, close connections
51 | // that have been idle longer than 30 sec
52 | connMgr.closeIdleConnections(30, TimeUnit.SECONDS);
53 | }
54 | }
55 | } catch (InterruptedException ex) {
56 | // terminate
57 | }
58 | }
59 |
60 | public synchronized void tryStart() {
61 | if(!super.isAlive()){
62 | super.start();
63 | }
64 | }
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/BaseApacheClient.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import org.apache.http.HttpHost;
29 | import org.apache.http.client.config.RequestConfig;
30 |
31 | abstract class BaseApacheClient {
32 |
33 | protected RequestConfig getRequestConfig(Config config) {
34 | Integer connectionTimeout = config.getConnectionTimeout();
35 | Integer socketTimeout = config.getSocketTimeout();
36 | HttpHost proxy = config.getProxy();
37 | return RequestConfig.custom()
38 | .setConnectTimeout(connectionTimeout)
39 | .setSocketTimeout(socketTimeout)
40 | .setConnectionRequestTimeout(socketTimeout)
41 | .setProxy(proxy)
42 | .build();
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/BaseResponse.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import java.io.InputStream;
29 | import java.util.Optional;
30 | import java.util.function.Consumer;
31 | import java.util.function.Function;
32 |
33 | abstract class BaseResponse implements HttpResponse {
34 |
35 | private final Headers headers;
36 | private final String statusText;
37 | private final int statusCode;
38 | private Optional parsingerror = Optional.empty();
39 |
40 | protected BaseResponse(RawResponse response){
41 | headers = response.getHeaders();
42 | this.statusCode = response.getStatus();
43 | this.statusText = response.getStatusText();
44 | }
45 |
46 | @Override
47 | public int getStatus() {
48 | return statusCode;
49 | }
50 |
51 | @Override
52 | public String getStatusText() {
53 | return statusText;
54 | }
55 |
56 | @Override
57 | public Headers getHeaders() {
58 | return headers;
59 | }
60 |
61 | @Override
62 | public abstract InputStream getRawBody();
63 |
64 | @Override
65 | public abstract T getBody();
66 |
67 | @Override
68 | public Optional getParsingError() {
69 | return parsingerror;
70 | }
71 |
72 | @Override
73 | public V mapBody(Function func){
74 | return func.apply(getBody());
75 | }
76 |
77 | @Override
78 | public V mapRawBody(Function func) {
79 | return func.apply(getRawBody());
80 | }
81 |
82 | protected void setParsingException(String originalBody, RuntimeException e) {
83 | parsingerror = Optional.of(new UnirestParsingException(originalBody, e));
84 | }
85 |
86 | @Override
87 | public HttpResponse ifSuccess(Consumer> consumer) {
88 | if(isSuccess()){
89 | consumer.accept(this);
90 | }
91 | return this;
92 | }
93 |
94 | @Override
95 | public HttpResponse ifFailure(Consumer> consumer) {
96 | if(!isSuccess()){
97 | consumer.accept(this);
98 | }
99 | return this;
100 | }
101 |
102 | @Override
103 | public boolean isSuccess() {
104 | return getStatus() >= 200 && getStatus() < 300 && !getParsingError().isPresent();
105 | }
106 | }
107 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/BasicResponse.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import java.io.InputStream;
29 |
30 | class BasicResponse extends BaseResponse {
31 | private final T body;
32 |
33 | public BasicResponse(RawResponse httpResponse, T body) {
34 | super(httpResponse);
35 | this.body = body;
36 | }
37 |
38 | @Override
39 | public InputStream getRawBody() {
40 | return null;
41 | }
42 |
43 | @Override
44 | public T getBody() {
45 | return body;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/BinaryResponse.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import java.io.ByteArrayInputStream;
29 | import java.io.InputStream;
30 |
31 | public class BinaryResponse extends BaseResponse {
32 |
33 | private InputStream body;
34 |
35 | protected BinaryResponse(RawResponse response) {
36 | super(response);
37 | body = new ByteArrayInputStream(response.getContentAsBytes());
38 | }
39 |
40 | @Override
41 | public InputStream getRawBody() {
42 | return body;
43 | }
44 |
45 | @Override
46 | public InputStream getBody() {
47 | return body;
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/Body.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import org.apache.http.HttpEntity;
29 |
30 | public interface Body {
31 |
32 | @Deprecated // In version 4 Apache classes will be abstracted out
33 | HttpEntity getEntity();
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/Callback.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | public interface Callback {
29 | void completed(HttpResponse response);
30 |
31 | default void failed(UnirestException e){}
32 |
33 | default void cancelled(){}
34 | }
35 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/CallbackFuture.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import java.util.concurrent.CompletableFuture;
29 |
30 | class CallbackFuture {
31 |
32 | static CompletableFuture> wrap(Callback source){
33 | return new CompletableFuture>(){
34 | @Override
35 | public boolean complete(HttpResponse value) {
36 | source.completed(value);
37 | return super.complete(value);
38 | }
39 | @Override
40 | public boolean completeExceptionally(Throwable ex) {
41 | source.failed(new UnirestException(ex));
42 | return super.completeExceptionally(ex);
43 | }
44 | @Override
45 | public boolean cancel(boolean mayInterruptIfRunning) {
46 | source.cancelled();
47 | return super.cancel(mayInterruptIfRunning);
48 | }
49 | };
50 | }
51 | }
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/Client.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import org.apache.http.client.HttpClient;
29 |
30 | import java.util.function.Function;
31 | import java.util.stream.Stream;
32 |
33 | public interface Client {
34 | /**
35 | * @return the apache HttpClient.
36 | * @deprecated Eventually the apache implementation will be hidden and other implementation wi be available
37 | */
38 | HttpClient getClient();
39 |
40 | /**
41 | * Make a request
42 | * @param The type of the body
43 | * @param request the prepared request object
44 | * @param transformer the function to transform the response
45 | * @return a HttpResponse with a transformed body
46 | */
47 | HttpResponse request(HttpRequest request, Function> transformer);
48 |
49 | /**
50 | * @return a stream of exceptions possibly thrown while closing all the things.
51 | */
52 | Stream close();
53 | }
54 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/Empty.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | public class Empty {
29 | private Empty(){}
30 | }
31 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/EmptyResponse.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import java.io.InputStream;
29 |
30 | class EmptyResponse extends BaseResponse {
31 | EmptyResponse(RawResponse response) {
32 | super(response);
33 | }
34 |
35 | @Override
36 | public InputStream getRawBody() {
37 | return null;
38 | }
39 |
40 | @Override
41 | public Empty getBody() {
42 | return null;
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/FileResponse.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import java.io.ByteArrayInputStream;
29 | import java.io.File;
30 | import java.io.IOException;
31 | import java.io.InputStream;
32 | import java.nio.file.Files;
33 | import java.nio.file.Path;
34 | import java.nio.file.Paths;
35 |
36 | public class FileResponse extends BaseResponse {
37 | private File body;
38 |
39 | public FileResponse(RawResponse r, String path) {
40 | super(r);
41 | try {
42 | Path target = Paths.get(path);
43 | Files.copy(r.getContent(), target);
44 | body = target.toFile();
45 | } catch (IOException e) {
46 | throw new UnirestException(e);
47 | }
48 | }
49 |
50 | @Override
51 | public InputStream getRawBody() {
52 | return new ByteArrayInputStream(new byte[0]);
53 | }
54 |
55 | @Override
56 | public File getBody() {
57 | return body;
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/FormPart.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import org.apache.http.entity.ContentType;
29 | import org.apache.http.entity.mime.content.*;
30 |
31 | import java.io.File;
32 | import java.nio.charset.StandardCharsets;
33 |
34 | class FormPart implements Comparable {
35 | private final String name;
36 | private final Object value;
37 | private final ContentType contentType;
38 |
39 | FormPart(String name, Object value, ContentType contentType) {
40 | this.name = name;
41 | this.value = value;
42 | this.contentType = contentType;
43 | }
44 |
45 | public Object getValue() {
46 | return value;
47 | }
48 |
49 | private ContentType getContentType() {
50 | if(contentType == null){
51 | if(isFile()){
52 | return ContentType.APPLICATION_OCTET_STREAM;
53 | }
54 | return ContentType.APPLICATION_FORM_URLENCODED.withCharset(StandardCharsets.UTF_8);
55 | }
56 | return contentType;
57 | }
58 |
59 | ContentBody toApachePart() {
60 | if (value instanceof File) {
61 | File file = (File) value;
62 | return new FileBody(file, getContentType(), file.getName());
63 | } else if (value instanceof InputStreamBody) {
64 | return (ContentBody) value;
65 | } else if (value instanceof ByteArrayBody) {
66 | return (ContentBody) value;
67 | } else {
68 | return new StringBody(value.toString(), getContentType());
69 | }
70 | }
71 |
72 | public String getName() {
73 | return name == null ? "" : name;
74 | }
75 |
76 | @Override
77 | public int compareTo(Object o) {
78 | if(o instanceof FormPart){
79 | return getName().compareTo(((FormPart)o).getName());
80 | }
81 | return 0;
82 | }
83 |
84 | public boolean isFile(){
85 | return value instanceof File
86 | || value instanceof InputStreamBody
87 | || value instanceof ByteArrayBody;
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/GenericType.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import java.lang.reflect.ParameterizedType;
29 | import java.lang.reflect.Type;
30 |
31 | /*
32 | Parts of this file were taken from Jackson/core TypeReference under the Apache License:
33 |
34 | Apache (Software) License, version 2.0 ("the License").
35 | See the License for details about distribution rights, and the
36 | specific rights regarding derivate works.
37 |
38 | You may obtain a copy of the License at:
39 |
40 | http://www.apache.org/licenses/LICENSE-2.0
41 |
42 | */
43 | public abstract class GenericType implements Comparable> {
44 | protected final Type type;
45 |
46 | protected GenericType() {
47 | Type superClass = this.getClass().getGenericSuperclass();
48 | if (superClass instanceof Class) {
49 | throw new IllegalArgumentException("Internal error: TypeReference constructed without actual type information");
50 | } else {
51 | this.type = ((ParameterizedType)superClass).getActualTypeArguments()[0];
52 | }
53 | }
54 |
55 | public Type getType() {
56 | return this.type;
57 | }
58 |
59 | public int compareTo(GenericType o) {
60 | return 0;
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/GetRequest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | public interface GetRequest extends HttpRequest {
29 | }
30 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/Header.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | public interface Header {
29 | String getName();
30 | String getValue();
31 | }
32 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/HttpDeleteWithBody.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
29 |
30 | import java.net.URI;
31 |
32 | class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {
33 | private static final String METHOD_NAME = "DELETE";
34 |
35 | public String getMethod() {
36 | return METHOD_NAME;
37 | }
38 |
39 | HttpDeleteWithBody(final String uri) {
40 | super();
41 | setURI(URI.create(uri));
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/HttpMethod.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import java.util.HashMap;
29 | import java.util.HashSet;
30 | import java.util.Map;
31 | import java.util.Set;
32 |
33 | public class HttpMethod {
34 | private static final Map REGISTRY = new HashMap<>();
35 |
36 | public static final HttpMethod GET = valueOf("GET");
37 | public static final HttpMethod POST = valueOf("POST");
38 | public static final HttpMethod PUT = valueOf("PUT");
39 | public static final HttpMethod DELETE = valueOf("DELETE");
40 | public static final HttpMethod PATCH = valueOf("PATCH");
41 | public static final HttpMethod HEAD = valueOf("HEAD");
42 | public static final HttpMethod OPTIONS = valueOf("OPTIONS");
43 | public static final HttpMethod TRACE = valueOf("TRACE");
44 |
45 | private final String name;
46 |
47 | private HttpMethod(String name){
48 | this.name = name;
49 | }
50 |
51 | public static HttpMethod valueOf(String verb){
52 | return REGISTRY.computeIfAbsent(verb, HttpMethod::new);
53 | }
54 |
55 | public Set all(){
56 | return new HashSet<>(REGISTRY.values());
57 | }
58 |
59 | public String name() {
60 | return name;
61 | }
62 |
63 | @Override
64 | public String toString() {
65 | return name;
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/HttpPatchWithBody.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
29 |
30 | import java.net.URI;
31 |
32 | class HttpPatchWithBody extends HttpEntityEnclosingRequestBase {
33 | private static final String METHOD_NAME = "PATCH";
34 |
35 | public String getMethod() {
36 | return METHOD_NAME;
37 | }
38 |
39 | HttpPatchWithBody(final String uri) {
40 | super();
41 | setURI(URI.create(uri));
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/HttpRequestJsonPatch.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import org.apache.http.HttpEntity;
29 | import org.apache.http.entity.BasicHttpEntity;
30 |
31 | import java.io.ByteArrayInputStream;
32 |
33 | class HttpRequestJsonPatch extends BaseRequest implements JsonPatchRequest {
34 |
35 | private JsonPatch items = new JsonPatch();
36 |
37 | HttpRequestJsonPatch(Config config, String url) {
38 | super(config, HttpMethod.PATCH, url);
39 | header("Content-Type", CONTENT_TYPE);
40 | }
41 |
42 | @Override
43 | public JsonPatchRequest add(String path, Object value) {
44 | items.add(path, value);
45 | return this;
46 | }
47 |
48 | @Override
49 | public JsonPatchRequest remove(String path) {
50 | items.remove(path);
51 | return this;
52 | }
53 |
54 | @Override
55 | public JsonPatchRequest replace(String path, Object value) {
56 | items.replace(path, value);
57 | return this;
58 | }
59 |
60 | @Override
61 | public JsonPatchRequest test(String path, Object value) {
62 | items.test(path, value);
63 | return this;
64 | }
65 |
66 | @Override
67 | public JsonPatchRequest move(String from, String path) {
68 | items.move(from, path);
69 | return this;
70 | }
71 |
72 | @Override
73 | public JsonPatchRequest copy(String from, String path) {
74 | items.copy(from, path);
75 | return this;
76 | }
77 |
78 | @Override
79 | public Body getBody() {
80 | return this;
81 | }
82 |
83 | @Override
84 | public HttpEntity getEntity() {
85 | BasicHttpEntity e = new BasicHttpEntity();
86 | e.setContent(new ByteArrayInputStream(items.toString().getBytes()));
87 | return e;
88 | }
89 | }
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/HttpRequestNoBody.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | class HttpRequestNoBody extends BaseRequest implements GetRequest {
29 | HttpRequestNoBody(Config config, HttpMethod method, String url) {
30 | super(config, method, url);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/HttpRequestUniBody.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import org.apache.http.HttpEntity;
29 | import org.apache.http.entity.ByteArrayEntity;
30 | import org.apache.http.entity.StringEntity;
31 |
32 | import java.nio.charset.Charset;
33 | import java.nio.charset.StandardCharsets;
34 | import java.util.function.Supplier;
35 |
36 | class HttpRequestUniBody extends BaseRequest implements RequestBodyEntity {
37 |
38 | private Supplier body = () -> new StringEntity("", StandardCharsets.UTF_8);
39 | private Charset charSet;
40 |
41 | HttpRequestUniBody(HttpRequestBody httpRequest) {
42 | super(httpRequest);
43 | this.charSet = httpRequest.getCharset();
44 | }
45 |
46 | @Override
47 | public RequestBodyEntity body(byte[] bodyBytes) {
48 | this.body = () -> new ByteArrayEntity(bodyBytes);
49 | return this;
50 | }
51 |
52 | @Override
53 | public RequestBodyEntity body(String bodyAsString) {
54 | this.body = () -> new StringEntity(bodyAsString, charSet);
55 | return this;
56 | }
57 |
58 | @Override
59 | public RequestBodyEntity body(JsonNode jsonBody) {
60 | return body(jsonBody.toString());
61 | }
62 |
63 | @Override
64 | public RequestBodyEntity charset(Charset charset) {
65 | this.charSet = charset;
66 | return this;
67 | }
68 |
69 | @Override
70 | public HttpEntity getEntity() {
71 | return body.get();
72 | }
73 |
74 | @Override
75 | public Body getBody() {
76 | return this;
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/HttpRequestWithBody.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import org.apache.http.entity.ContentType;
29 | import org.json.JSONArray;
30 | import org.json.JSONObject;
31 |
32 | import java.io.File;
33 | import java.io.InputStream;
34 | import java.nio.charset.Charset;
35 | import java.util.Collection;
36 | import java.util.Map;
37 |
38 | public interface HttpRequestWithBody extends HttpRequest {
39 | HttpRequestWithBody charset(Charset charset);
40 |
41 | MultipartBody field(String name, Collection> value);
42 |
43 | MultipartBody field(String name, File file);
44 |
45 | MultipartBody field(String name, File file, String contentType);
46 |
47 | MultipartBody field(String name, Object value);
48 |
49 | MultipartBody field(String name, Object value, String contentType);
50 |
51 | MultipartBody fields(Map parameters);
52 |
53 | MultipartBody field(String name, InputStream stream, ContentType contentType, String fileName);
54 |
55 | MultipartBody field(String name, InputStream stream, String fileName);
56 |
57 | RequestBodyEntity body(JsonNode body);
58 |
59 | RequestBodyEntity body(String body);
60 |
61 | RequestBodyEntity body(Object body);
62 |
63 | RequestBodyEntity body(byte[] body);
64 |
65 | RequestBodyEntity body(JSONObject body);
66 |
67 | RequestBodyEntity body(JSONArray body);
68 |
69 | Charset getCharset();
70 | }
71 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/HttpResponse.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import java.io.InputStream;
29 | import java.util.Optional;
30 | import java.util.function.Consumer;
31 | import java.util.function.Function;
32 |
33 | /**
34 | * @param a Http Response holding a specific type of body.
35 | */
36 | public interface HttpResponse {
37 |
38 | /**
39 | * @return the HTTP status code.
40 | */
41 | int getStatus();
42 |
43 | /**
44 | * @return status text
45 | */
46 | String getStatusText();
47 |
48 | /**
49 | * @return Response Headers (map) with same case as server response.
50 | * For instance use getHeaders().getFirst("Location")
and not getHeaders().getFirst("location")
to get first header "Location"
51 | */
52 | Headers getHeaders();
53 |
54 | /**
55 | * This method is a lie. You never get the real raw response from it
56 | * you only get a copy, or worse, the body transformed BACK to a stream
57 | * If you want to use raw content use the new functional methods
58 | * @deprecated this method is redundant and not the original stream. Use the functional asObject methods.
59 | * @return a copy of the input stream
60 | * */
61 | @Deprecated
62 | InputStream getRawBody();
63 |
64 | /**
65 | * @return the body
66 | */
67 | T getBody();
68 |
69 | /**
70 | * If the transformation to the body failed by an exception it will be kept here
71 | * @return a possible RuntimeException. Checked exceptions are wrapped in a UnirestException
72 | */
73 | Optional getParsingError();
74 |
75 | /**
76 | * @param func a function to transform a body type to something else.
77 | * @param The return type of the function
78 | * @return the return type
79 | */
80 | V mapBody(Function func);
81 |
82 | /**
83 | * This method is a lie. You never get the real raw response from it
84 | * you only get a copy, or worse, the body transformed BACK to a stream
85 | * If you want to use raw content use the new functional methods
86 | * @param func a function to map a inputstream to a new body
87 | * @param the return type
88 | * @return the return type
89 | */
90 | @Deprecated
91 | V mapRawBody(Function func);
92 |
93 | /**
94 | * If the response was a 200-series response. Invoke this consumer
95 | * can be chained with ifFailure
96 | * @param consumer a function to consume a HttpResponse
97 | * @return the same response
98 | */
99 | HttpResponse ifSuccess(Consumer> consumer);
100 |
101 | /**
102 | * If the response was NOT a 200-series response or a mapping exception happened. Invoke this consumer
103 | * can be chained with ifSuccess
104 | * @param consumer a function to consume a HttpResponse
105 | * @return the same response
106 | */
107 | HttpResponse ifFailure(Consumer> consumer);
108 |
109 | /**
110 | * @return true if the response was a 200-series response and no mapping exception happened, else false
111 | */
112 | boolean isSuccess();
113 | }
114 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/JsonNode.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import org.json.JSONArray;
29 | import org.json.JSONException;
30 | import org.json.JSONObject;
31 |
32 | public class JsonNode {
33 |
34 | private JSONObject jsonObject;
35 | private JSONArray jsonArray;
36 |
37 | private boolean array;
38 |
39 | public JsonNode(String json) {
40 | if (json == null || "".equals(json.trim())) {
41 | jsonObject = new JSONObject();
42 | } else {
43 | try {
44 | jsonObject = new JSONObject(json);
45 | } catch (JSONException e) {
46 | // It may be an array
47 | jsonArray = new JSONArray(json);
48 | array = true;
49 | }
50 | }
51 | }
52 |
53 | public JSONObject getObject() {
54 | return this.jsonObject;
55 | }
56 |
57 | public JSONArray getArray() {
58 | JSONArray result = this.jsonArray;
59 | if (array == false) {
60 | result = new JSONArray();
61 | result.put(jsonObject);
62 | }
63 | return result;
64 | }
65 |
66 | public boolean isArray() {
67 | return this.array;
68 | }
69 |
70 | @Override
71 | public String toString() {
72 | if (isArray()) {
73 | return jsonArray.toString();
74 | } else {
75 | return jsonObject.toString();
76 | }
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/JsonPatch.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import org.json.JSONArray;
29 | import org.json.JSONObject;
30 |
31 | import java.util.ArrayList;
32 | import java.util.Collections;
33 | import java.util.List;
34 |
35 | import static unirest.JsonPatchOperation.*;
36 |
37 | public class JsonPatch {
38 |
39 | private List items = new ArrayList<>();
40 |
41 | public JsonPatch(){}
42 |
43 | public JsonPatch(String fromString){
44 | for (Object row : new JSONArray(fromString)) {
45 | if(row instanceof JSONObject){
46 | items.add(new JsonPatchItem((JSONObject)row));
47 | }
48 | }
49 | }
50 |
51 | public void add(String path, Object value) {
52 | items.add(new JsonPatchItem(add, path, value));
53 | }
54 |
55 | public void remove(String path) {
56 | items.add(new JsonPatchItem(remove, path));
57 | }
58 |
59 | public void replace(String path, Object value) {
60 | items.add(new JsonPatchItem(replace, path, value));
61 | }
62 |
63 | public void test(String path, Object value) {
64 | items.add(new JsonPatchItem(test, path, value));
65 | }
66 |
67 | public void move(String from, String path) {
68 | items.add(new JsonPatchItem(move, path, from));
69 | }
70 |
71 | public void copy(String from, String path) {
72 | items.add(new JsonPatchItem(copy, path, from));
73 | }
74 |
75 | @Override
76 | public String toString() {
77 | JSONArray a = new JSONArray();
78 | items.forEach(i -> a.put(new JSONObject(i.toString())));
79 | return a.toString();
80 | }
81 |
82 | public Iterable getOperations(){
83 | return Collections.unmodifiableList(items);
84 | }
85 | }
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/JsonPatchItem.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import org.json.JSONObject;
29 |
30 | import java.util.Objects;
31 |
32 | public class JsonPatchItem {
33 | private final JsonPatchOperation op;
34 | private final String path;
35 | private final Object value;
36 |
37 | public JsonPatchItem(JsonPatchOperation op, String path, Object value){
38 | this.op = op;
39 | this.path = path;
40 | this.value = value;
41 | }
42 |
43 | public JsonPatchItem(JsonPatchOperation remove, String path) {
44 | this(remove, path, null);
45 | }
46 |
47 | public JsonPatchItem(JSONObject row) {
48 | this.op = JsonPatchOperation.valueOf(row.getString("op"));
49 | this.path = row.getString("path");
50 | if(row.has(op.getOperationtype())) {
51 | this.value = row.get(op.getOperationtype());
52 | } else {
53 | this.value = null;
54 | }
55 | }
56 |
57 | @Override
58 | public boolean equals(Object o) {
59 | if (this == o) {return true;}
60 | if (o == null || getClass() != o.getClass()) {return false;}
61 | JsonPatchItem that = (JsonPatchItem) o;
62 | return op == that.op &&
63 | Objects.equals(path, that.path) &&
64 | Objects.equals(toString(), that.toString());
65 | }
66 |
67 | @Override
68 | public int hashCode() {
69 | return Objects.hash(op, path, value);
70 | }
71 |
72 | @Override
73 | public String toString() {
74 | JSONObject json = new JSONObject()
75 | .put("op", op)
76 | .put("path", path);
77 |
78 | if(Objects.nonNull(value)){
79 | json.put(op.getOperationtype(), value);
80 | }
81 |
82 | return json.toString();
83 | }
84 |
85 | public JsonPatchOperation getOp() {
86 | return op;
87 | }
88 |
89 | public String getPath() {
90 | return path;
91 | }
92 |
93 | public Object getValue() {
94 | return value;
95 | }
96 | }
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/JsonPatchOperation.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | public enum JsonPatchOperation {
29 | add("value"),
30 | remove("value"),
31 | replace("value"),
32 | test("value"),
33 | move("from"),
34 | copy("from");
35 |
36 | private final String operationtype;
37 |
38 | JsonPatchOperation(String operationtype) {
39 | this.operationtype = operationtype;
40 | }
41 |
42 | public String getOperationtype() {
43 | return operationtype;
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/JsonPatchRequest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | public interface JsonPatchRequest extends HttpRequest, Body {
29 | String CONTENT_TYPE = "application/json-patch+json";
30 |
31 | JsonPatchRequest add(String path, Object value);
32 |
33 | JsonPatchRequest remove(String path);
34 |
35 | JsonPatchRequest replace(String path, Object value);
36 |
37 | JsonPatchRequest test(String path, Object value);
38 |
39 | JsonPatchRequest move(String from, String path);
40 |
41 | JsonPatchRequest copy(String from, String path);
42 | }
43 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/JsonResponse.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import java.io.ByteArrayInputStream;
29 | import java.io.InputStream;
30 | import java.util.Objects;
31 |
32 | public class JsonResponse extends BaseResponse {
33 | private JsonNode node;
34 | private InputStream errorStream;
35 |
36 | protected JsonResponse(RawResponse response) {
37 | super(response);
38 | node = getNode(response);
39 | }
40 |
41 | private JsonNode getNode(RawResponse response) {
42 | if (Objects.isNull(response) || !response.hasContent()) {
43 | return new JsonNode(null);
44 | } else {
45 | String json = response.getContentAsString();
46 | return toJsonNode(json);
47 | }
48 | }
49 |
50 | private JsonNode toJsonNode(String json) {
51 | try {
52 | return new JsonNode(json);
53 | } catch (RuntimeException e) {
54 | super.setParsingException(json, e);
55 | errorStream = new ByteArrayInputStream(json.getBytes());
56 | return null;
57 | }
58 | }
59 |
60 | @Override
61 | public InputStream getRawBody() {
62 | if (errorStream != null) {
63 | return errorStream;
64 | }
65 | return new ByteArrayInputStream(node.toString().getBytes());
66 | }
67 |
68 | @Override
69 | public JsonNode getBody() {
70 | return node;
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/NoRedirects.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import org.apache.http.HttpRequest;
29 | import org.apache.http.HttpResponse;
30 | import org.apache.http.client.RedirectStrategy;
31 | import org.apache.http.client.methods.HttpUriRequest;
32 | import org.apache.http.protocol.HttpContext;
33 |
34 | class NoRedirects implements RedirectStrategy {
35 | @Override
36 | public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) {
37 | return false;
38 | }
39 |
40 | @Override
41 | public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) {
42 | return null;
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/ObjectMapper.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | public interface ObjectMapper {
29 | T readValue(String value, Class valueType);
30 | default T readValue(String value, GenericType genericType){
31 | throw new UnirestException("Please implement me");
32 | }
33 | String writeValue(Object value);
34 | }
35 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/ObjectResponse.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import java.io.ByteArrayInputStream;
29 | import java.io.InputStream;
30 | import java.util.Optional;
31 | import java.util.function.Function;
32 |
33 |
34 | class ObjectResponse extends BaseResponse {
35 | private final T body;
36 | private final ObjectMapper om;
37 |
38 | ObjectResponse(ObjectMapper om, RawResponse response, Class extends T> to) {
39 | super(response);
40 | this.om = om;
41 | this.body = readBody(response)
42 | .map(s -> getBody(s, e -> om.readValue(e, to)))
43 | .orElse(null);
44 | }
45 |
46 | ObjectResponse(ObjectMapper om, RawResponse response, GenericType extends T> to){
47 | super(response);
48 | this.om = om;
49 | this.body = readBody(response)
50 | .map(s -> getBody(s, e -> om.readValue(e, to)))
51 | .orElse(null);
52 | }
53 |
54 | private Optional readBody(RawResponse response) {
55 | if(!response.hasContent()){
56 | return Optional.empty();
57 | }
58 | return Optional.of(response.getContentAsString());
59 | }
60 |
61 | private T getBody(String b, Function func){
62 | try {
63 | return func.apply(b);
64 | } catch (RuntimeException e) {
65 | setParsingException(b, e);
66 | return null;
67 | }
68 | }
69 |
70 | @Override
71 | public InputStream getRawBody() {
72 | if(body == null){
73 | return new ByteArrayInputStream(new byte[0]);
74 | }
75 | return new ByteArrayInputStream(om.writeValue(body).getBytes());
76 | }
77 |
78 | @Override
79 | public T getBody() {
80 | return body;
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/PagedList.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import java.util.ArrayList;
29 | import java.util.List;
30 | import java.util.function.Consumer;
31 | import java.util.stream.Collectors;
32 |
33 | public class PagedList extends ArrayList> {
34 |
35 | /**
36 | * @return Returns all successful bodies
37 | */
38 | public List getBodies() {
39 | return stream()
40 | .filter(HttpResponse::isSuccess)
41 | .map(HttpResponse::getBody)
42 | .collect(Collectors.toList());
43 | }
44 |
45 | /**
46 | * For each successful response If the response was a 200-series response. Invoke this consumer
47 | * can be chained with ifFailure
48 | * @param consumer a function to consume a HttpResponse
49 | * @return the same paged list
50 | */
51 | public PagedList ifSuccess(Consumer> consumer) {
52 | stream().filter(HttpResponse::isSuccess).forEach(consumer);
53 | return this;
54 | }
55 |
56 | /**
57 | * For each failed response if the response was NOT a 200-series response or a mapping exception happened. Invoke this consumer
58 | * can be chained with ifSuccess
59 | * @param consumer a function to consume a HttpResponse
60 | * @return the same paged list
61 | */
62 | public PagedList ifFailure(Consumer> consumer) {
63 | stream().filter(r -> !r.isSuccess()).forEach(consumer);
64 | return this;
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/Path.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import java.io.UnsupportedEncodingException;
29 | import java.net.URLEncoder;
30 | import java.util.Collection;
31 | import java.util.Map;
32 | import java.util.regex.Matcher;
33 | import java.util.regex.Pattern;
34 |
35 | class Path {
36 | private String url;
37 |
38 | Path(String url) {
39 | this.url = url;
40 | }
41 |
42 | public void param(String name, String value) {
43 | Matcher matcher = Pattern.compile("\\{" + name + "\\}").matcher(url);
44 | int count = 0;
45 | while (matcher.find()) {
46 | count++;
47 | }
48 | if (count == 0) {
49 | throw new UnirestException("Can't find route parameter name \"" + name + "\"");
50 | }
51 | this.url = url.replaceAll("\\{" + name + "\\}", Util.encode(value));
52 | }
53 |
54 | public void queryString(String name, Collection> value){
55 | for (Object cur : value) {
56 | queryString(name, cur);
57 | }
58 | }
59 |
60 | public void queryString(String name, Object value) {
61 | StringBuilder queryString = new StringBuilder();
62 | if (url.contains("?")) {
63 | queryString.append("&");
64 | } else {
65 | queryString.append("?");
66 | }
67 | try {
68 | queryString.append(URLEncoder.encode(name));
69 | if(value != null) {
70 | queryString.append("=").append(URLEncoder.encode(value.toString(), "UTF-8"));
71 | }
72 | } catch (UnsupportedEncodingException e) {
73 | throw new UnirestException(e);
74 | }
75 | url += queryString.toString();
76 | }
77 |
78 | public void queryString(Map parameters) {
79 | if (parameters != null) {
80 | for (Map.Entry param : parameters.entrySet()) {
81 | if (param.getValue() instanceof String || param.getValue() instanceof Number || param.getValue() instanceof Boolean || param.getValue() == null) {
82 | queryString(param.getKey(), param.getValue());
83 | } else {
84 | throw new UnirestException("Parameter \"" + param.getKey() +
85 | "\" can't be sent with a GET request because of type: "
86 | + param.getValue().getClass().getName());
87 | }
88 | }
89 | }
90 | }
91 |
92 | @Override
93 | public String toString() {
94 | return url;
95 | }
96 | }
97 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/RawResponse.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import java.io.InputStream;
29 | import java.io.InputStreamReader;
30 |
31 | public interface RawResponse {
32 | int getStatus();
33 | String getStatusText();
34 | Headers getHeaders();
35 | InputStream getContent();
36 | byte[] getContentAsBytes();
37 | String getContentAsString();
38 | String getContentAsString(String charset);
39 | InputStreamReader getContentReader();
40 | boolean hasContent();
41 | String getContentType();
42 | String getEncoding();
43 | }
44 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/RawResponseBase.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import java.util.regex.Matcher;
29 | import java.util.regex.Pattern;
30 |
31 | abstract class RawResponseBase implements RawResponse {
32 |
33 | private static final Pattern CHARSET_PATTERN = Pattern.compile("(?i)\\bcharset=\\s*\"?([^\\s;\"]*)");
34 | protected Config config;
35 |
36 | protected RawResponseBase(Config config){
37 | this.config = config;
38 | }
39 |
40 | protected String getCharSet() {
41 | String contentType = getContentType();
42 | String responseCharset = getCharsetFromContentType(contentType);
43 | if (responseCharset != null && !responseCharset.trim().equals("")) {
44 | return responseCharset;
45 | }
46 | return config.getDefaultResponseEncoding();
47 | }
48 |
49 | /**
50 | * Parse out a charset from a content type header.
51 | *
52 | * @param contentType e.g. "text/html; charset=EUC-JP"
53 | * @return "EUC-JP", or null if not found. Charset is trimmed and uppercased.
54 | */
55 | private String getCharsetFromContentType(String contentType) {
56 | if (contentType == null) {
57 | return null;
58 | }
59 |
60 | Matcher m = CHARSET_PATTERN.matcher(contentType);
61 | if (m.find()) {
62 | return m.group(1).trim().toUpperCase();
63 | }
64 | return null;
65 | }
66 |
67 | }
68 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/RequestBodyEntity.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import java.nio.charset.Charset;
29 |
30 | public interface RequestBodyEntity extends HttpRequest, Body {
31 | RequestBodyEntity body(byte[] bodyBytes);
32 |
33 | RequestBodyEntity body(String bodyAsString);
34 |
35 | RequestBodyEntity body(JsonNode jsonBody);
36 |
37 | RequestBodyEntity charset(Charset charset);
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/StringResponse.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import java.io.ByteArrayInputStream;
29 | import java.io.InputStream;
30 |
31 |
32 | public class StringResponse extends BaseResponse {
33 | private String body;
34 |
35 | public StringResponse(RawResponse response, String encoding) {
36 | super(response);
37 | body = response.getContentAsString(encoding);
38 | }
39 |
40 | @Override
41 | public InputStream getRawBody() {
42 | return new ByteArrayInputStream(body.getBytes());
43 | }
44 |
45 | @Override
46 | public String getBody() {
47 | return body;
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/SyncIdleConnectionMonitorThread.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import java.util.concurrent.TimeUnit;
29 |
30 | import org.apache.http.conn.HttpClientConnectionManager;
31 |
32 | public class SyncIdleConnectionMonitorThread extends Thread {
33 |
34 | private final HttpClientConnectionManager connMgr;
35 |
36 | public SyncIdleConnectionMonitorThread(HttpClientConnectionManager connMgr) {
37 | super();
38 | super.setDaemon(true);
39 | this.connMgr = connMgr;
40 | }
41 |
42 | @Override
43 | public void run() {
44 | try {
45 | while (!Thread.currentThread().isInterrupted()) {
46 | synchronized (this) {
47 | wait(5000);
48 | // Close expired connections
49 | connMgr.closeExpiredConnections();
50 | // Optionally, close connections
51 | // that have been idle longer than 30 sec
52 | connMgr.closeIdleConnections(30, TimeUnit.SECONDS);
53 | }
54 | }
55 | } catch (InterruptedException ex) {
56 | // terminate
57 | }
58 | }
59 |
60 | }
61 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/UnirestConfigException.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | public class UnirestConfigException extends UnirestException {
29 | public UnirestConfigException(Exception e){
30 | super(e);
31 | }
32 |
33 | public UnirestConfigException(String msg) {
34 | super(msg);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/UnirestException.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import java.util.Collection;
29 | import java.util.stream.Collectors;
30 |
31 | public class UnirestException extends RuntimeException {
32 |
33 | private static final long serialVersionUID = -3714840499934575734L;
34 |
35 | public UnirestException(Exception e) {
36 | super(e);
37 | }
38 |
39 | public UnirestException(String msg) {
40 | super(msg);
41 | }
42 |
43 | public UnirestException(Throwable ex) {
44 | super(ex);
45 | }
46 |
47 | public UnirestException(Collection ex) {
48 | super(ex.stream().map(e -> e.getClass().getName() + " " + e.getMessage()).collect(Collectors.joining("\n")));
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/UnirestParsingException.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | public class UnirestParsingException extends UnirestException {
29 | private final String originalBody;
30 |
31 | public UnirestParsingException(String originalBody, Exception e) {
32 | super(e);
33 | this.originalBody = originalBody;
34 | }
35 |
36 | public String getOriginalBody() {
37 | return originalBody;
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/unirest/src/main/java/unirest/Util.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import org.apache.http.NameValuePair;
29 | import org.apache.http.message.BasicNameValuePair;
30 |
31 | import java.io.ByteArrayInputStream;
32 | import java.io.InputStream;
33 | import java.io.UnsupportedEncodingException;
34 | import java.net.URLEncoder;
35 | import java.util.*;
36 | import java.util.stream.Stream;
37 |
38 | class Util {
39 |
40 | //In Java 9 this has been added as Optional::stream. Remove this whenever we get there.
41 | static Stream stream(Optional opt) {
42 | return opt.map(Stream::of).orElseGet(Stream::empty);
43 | }
44 |
45 | static Optional tryCast(T original, Class too) {
46 | if (original != null && too.isAssignableFrom(original.getClass())) {
47 | return Optional.of((M) original);
48 | }
49 | return Optional.empty();
50 | }
51 |
52 | static Optional tryDo(T c, ExConsumer consumer) {
53 | try {
54 | if (Objects.nonNull(c)) {
55 | consumer.accept(c);
56 | }
57 | return Optional.empty();
58 | } catch (Exception e) {
59 | return Optional.of(e);
60 | }
61 | }
62 |
63 | static String nullToEmpty(Object v) {
64 | if(v == null){
65 | return "";
66 | }
67 | return String.valueOf(v);
68 | }
69 |
70 | static String encode(String input) {
71 | try {
72 | return URLEncoder.encode(input, "UTF-8");
73 | } catch (UnsupportedEncodingException e) {
74 | throw new UnirestException(e);
75 | }
76 | }
77 |
78 | static List getList(Collection parameters) {
79 | List result = new ArrayList<>();
80 | for (FormPart entry : parameters) {
81 | result.add(new BasicNameValuePair(entry.getName(), entry.getValue().toString()));
82 | }
83 | return result;
84 | }
85 |
86 | static Stream collectExceptions(Optional... ex) {
87 | return Stream.of(ex).flatMap(Util::stream);
88 | }
89 |
90 | public static InputStream emptyStream() {
91 | return new ByteArrayInputStream(new byte[0]);
92 | }
93 |
94 | public static boolean isNullOrEmpty(String s) {
95 | return s == null || s.trim().isEmpty();
96 | }
97 |
98 |
99 | @FunctionalInterface
100 | public interface ExConsumer{
101 | void accept(T t) throws Exception;
102 | }
103 | }
104 |
--------------------------------------------------------------------------------
/unirest/src/test/java/BehaviorTests/AsBinaryTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package BehaviorTests;
27 |
28 | import unirest.HttpResponse;
29 | import unirest.Unirest;
30 | import org.junit.Test;
31 | import unirest.TestUtil;
32 |
33 | import java.io.ByteArrayInputStream;
34 | import java.io.IOException;
35 | import java.io.InputStream;
36 | import java.util.concurrent.CompletableFuture;
37 |
38 | import static org.junit.Assert.assertEquals;
39 | import static org.junit.Assert.assertNull;
40 |
41 | public class AsBinaryTest extends BddTest {
42 | @Test
43 | public void whenNoBodyIsReturned() throws IOException {
44 | HttpResponse i = Unirest.get(MockServer.NOBODY).asBinary();
45 |
46 | assertEquals(200, i.getStatus());
47 | assertEquals(0, i.getBody().available());
48 | }
49 |
50 | @Test
51 | public void canGetBinaryResponse() {
52 | HttpResponse i = Unirest.get(MockServer.GET)
53 | .queryString("foo", "bar")
54 | .asBinary();
55 |
56 | RequestCapture cap = TestUtil.readValue(i.getBody(), RequestCapture.class);
57 | cap.assertParam("foo", "bar");
58 | }
59 |
60 | @Test
61 | public void canGetBinaryResponseAsync() throws Exception {
62 | CompletableFuture> r = Unirest.get(MockServer.GET)
63 | .queryString("foo", "bar")
64 | .asBinaryAsync();
65 |
66 | RequestCapture cap = TestUtil.readValue(r.get().getBody(), RequestCapture.class);
67 | cap.assertParam("foo", "bar");
68 | }
69 |
70 | @Test
71 | public void canGetBinaryResponseAsyncWithCallback() {
72 | Unirest.get(MockServer.GET)
73 | .queryString("foo", "bar")
74 | .asBinaryAsync(r -> {
75 | RequestCapture cap = TestUtil.readValue(r.getBody(), RequestCapture.class);
76 | cap.assertParam("foo", "bar");
77 | asyncSuccess();
78 | });
79 |
80 | assertAsync();
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/unirest/src/test/java/BehaviorTests/AsEmptyTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package BehaviorTests;
27 |
28 | import org.junit.Test;
29 | import unirest.HttpResponse;
30 | import unirest.Unirest;
31 |
32 | import static junit.framework.TestCase.assertEquals;
33 |
34 | public class AsEmptyTest extends BddTest {
35 |
36 | @Test
37 | public void canDoAEmptyRequestThatDoesNotParseBodyAtAll() {
38 | MockServer.addResponseHeader("foo", "bar");
39 | HttpResponse res = Unirest.get(MockServer.GET).asEmpty();
40 |
41 | assertEquals(200, res.getStatus());
42 | assertEquals(null, res.getBody());
43 | assertEquals("bar", res.getHeaders().getFirst("foo"));
44 | }
45 |
46 | @Test
47 | public void canDoEmptyAsync() throws Exception {
48 | MockServer.addResponseHeader("foo", "bar");
49 | HttpResponse res = Unirest.get(MockServer.GET).asEmptyAsync().get();
50 |
51 | assertEquals(200, res.getStatus());
52 | assertEquals(null, res.getBody());
53 | assertEquals("bar", res.getHeaders().getFirst("foo"));
54 | }
55 |
56 | @Test
57 | public void canDoEmptyAsyncWithCallback() {
58 | MockServer.addResponseHeader("foo", "bar");
59 |
60 | Unirest.get(MockServer.GET)
61 | .asEmptyAsync(res -> {
62 | assertEquals(200, res.getStatus());
63 | assertEquals(null, res.getBody());
64 | assertEquals("bar", res.getHeaders().getFirst("foo"));
65 | asyncSuccess();
66 | });
67 |
68 | assertAsync();
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/unirest/src/test/java/BehaviorTests/AsFileTests.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package BehaviorTests;
27 |
28 | import org.junit.Test;
29 | import unirest.JacksonObjectMapper;
30 | import unirest.TestUtil;
31 | import unirest.Unirest;
32 |
33 | import java.io.File;
34 | import java.nio.file.Files;
35 | import java.nio.file.Path;
36 | import java.nio.file.Paths;
37 |
38 | import static org.junit.Assert.assertEquals;
39 | import static org.junit.Assert.assertTrue;
40 |
41 | public class AsFileTests extends BddTest {
42 |
43 | private Path test = Paths.get("results.json");
44 | private JacksonObjectMapper om = new JacksonObjectMapper();
45 |
46 | @Override
47 | public void tearDown() {
48 | try {
49 | Files.delete(test);
50 | } catch (Exception e) { }
51 | }
52 |
53 | @Test
54 | public void canSaveContentsIntoFile() {
55 | File result = Unirest.get(MockServer.GET)
56 | .queryString("talking","heads")
57 | .queryString("param3", "こんにちは")
58 | .asFile(test.toString())
59 | .getBody();
60 |
61 | om.readValue(result, RequestCapture.class)
62 | .assertParam("talking", "heads")
63 | .assertParam("param3", "こんにちは")
64 | .assertStatus(200);
65 |
66 | assertEquals(test.toFile().getPath(), result.getPath());
67 | }
68 |
69 | @Test
70 | public void canSaveContentsIntoFileAsync() throws Exception {
71 | File result = Unirest.get(MockServer.GET)
72 | .queryString("talking","heads")
73 | .queryString("param3", "こんにちは")
74 | .asFileAsync(test.toString())
75 | .get()
76 | .getBody();
77 |
78 | om.readValue(result, RequestCapture.class)
79 | .assertParam("talking", "heads")
80 | .assertParam("param3", "こんにちは")
81 | .assertStatus(200);
82 |
83 | assertEquals(test.toFile().getPath(), result.getPath());
84 | }
85 |
86 | @Test
87 | public void canSaveContentsIntoFileAsyncWithCallback() throws Exception {
88 | Unirest.get(MockServer.GET)
89 | .queryString("talking","heads")
90 | .queryString("param3", "こんにちは")
91 | .asFileAsync(test.toString(), r -> {
92 | om.readValue(r.getBody(), RequestCapture.class)
93 | .assertParam("talking", "heads")
94 | .assertParam("param3", "こんにちは")
95 | .assertStatus(200);
96 | assertEquals(test.toFile().getPath(), r.getBody().getPath());
97 | asyncSuccess();
98 | });
99 |
100 | assertAsync();
101 | }
102 |
103 | @Test
104 | public void canDownloadABinaryFile() throws Exception {
105 | File f1 = TestUtil.rezFile("/image.jpg");
106 |
107 | File f2 = Unirest.get(MockServer.BINARYFILE)
108 | .asFile(test.toString())
109 | .getBody();
110 |
111 | assertTrue(com.google.common.io.Files.equal(f1, f2));
112 | }
113 | }
114 |
--------------------------------------------------------------------------------
/unirest/src/test/java/BehaviorTests/AsJsonTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package BehaviorTests;
27 |
28 | import unirest.HttpResponse;
29 | import unirest.JsonNode;
30 | import unirest.Unirest;
31 | import org.junit.Test;
32 | import unirest.MockCallback;
33 | import unirest.TestUtil;
34 |
35 | import java.io.InputStream;
36 | import java.util.concurrent.CompletableFuture;
37 |
38 | import static org.junit.Assert.assertEquals;
39 | import static org.junit.Assert.assertNull;
40 |
41 | public class AsJsonTest extends BddTest {
42 |
43 | @Test
44 | public void whenNoBodyIsReturned() {
45 | HttpResponse i = Unirest.get(MockServer.NOBODY).asJson();
46 |
47 | assertEquals(200, i.getStatus());
48 | assertEquals("{}", i.getBody().toString());
49 | }
50 |
51 | @Test
52 | public void canGetBinaryResponse() {
53 | HttpResponse i = Unirest.get(MockServer.GET)
54 | .queryString("foo", "bar")
55 | .asJson();
56 |
57 | assertJson(i);
58 | }
59 |
60 | @Test
61 | public void canGetBinaryResponseAsync() throws Exception {
62 | CompletableFuture> r = Unirest.get(MockServer.GET)
63 | .queryString("foo", "bar")
64 | .asJsonAsync();
65 |
66 | assertJson(r.get());
67 | }
68 |
69 | @Test
70 | public void canGetBinaryResponseAsyncWithCallback() {
71 | Unirest.get(MockServer.GET)
72 | .queryString("foo", "bar")
73 | .asJsonAsync(r -> {
74 | assertJson(r);
75 | asyncSuccess();
76 | });
77 |
78 | assertAsync();
79 | }
80 |
81 | @Test
82 | public void failureToReturnValidJsonWillResultInAnEmptyNode() {
83 | HttpResponse response = Unirest.get(MockServer.INVALID_REQUEST).asJson();
84 |
85 | assertEquals(400, response.getStatus());
86 | assertNull(response.getBody());
87 | assertEquals("You did something bad", response.getParsingError().get().getOriginalBody());
88 | assertEquals("org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1]",
89 | response.getParsingError().get().getMessage());
90 | }
91 |
92 | @Test
93 | public void failureToReturnValidJsonWillResultInAnEmptyNodeAsync() {
94 | Unirest.get(MockServer.INVALID_REQUEST)
95 | .asJsonAsync(new MockCallback<>(this, response -> {
96 | assertEquals(400, response.getStatus());
97 | assertNull(response.getBody());
98 | assertEquals("You did something bad", TestUtil.toString(response.getRawBody()));
99 | assertEquals("org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1]",
100 | response.getParsingError().get().getMessage());
101 |
102 | }));
103 |
104 | assertAsync();
105 | }
106 |
107 | private void assertJson(HttpResponse i) {
108 | assertEquals("bar",i.getBody().getObject().getJSONObject("params").getJSONArray("foo").get(0));
109 | }
110 | }
111 |
--------------------------------------------------------------------------------
/unirest/src/test/java/BehaviorTests/BddTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package BehaviorTests;
27 |
28 | import org.junit.After;
29 | import unirest.*;
30 | import org.junit.Before;
31 |
32 | import java.util.concurrent.CountDownLatch;
33 | import java.util.concurrent.TimeUnit;
34 |
35 | import static unirest.TestUtil.read;
36 | import static org.junit.Assert.assertEquals;
37 | import static org.junit.Assert.assertFalse;
38 | import static org.junit.Assert.assertTrue;
39 |
40 | public class BddTest {
41 | private JacksonObjectMapper objectMapper = new JacksonObjectMapper();
42 | private CountDownLatch lock;
43 | private boolean status;
44 | private String fail;
45 |
46 | @Before
47 | public void setUp() {
48 | //TestUtil.debugApache();
49 | MockServer.reset();
50 | Unirest.config().setObjectMapper(objectMapper);
51 | lock = new CountDownLatch(1);
52 | status = false;
53 | }
54 |
55 | @After
56 | public void tearDown() {
57 | Unirest.shutDown(true);
58 | }
59 |
60 | public void assertAsync() {
61 | try {
62 | lock.await(5, TimeUnit.SECONDS);
63 | assertTrue("Expected a async call but it never responded", status);
64 | } catch (InterruptedException e) {
65 | throw new RuntimeException(e);
66 | }
67 | }
68 |
69 | public void assertFailed(String message) throws InterruptedException {
70 | lock.await(5, TimeUnit.SECONDS);
71 | assertFalse("Should have failed", status);
72 | assertEquals(message, fail);
73 | }
74 |
75 | public void asyncSuccess() {
76 | status = true;
77 | lock.countDown();
78 | }
79 |
80 | public void asyncFail(String message) {
81 | status = false;
82 | lock.countDown();
83 | fail = message;
84 | }
85 |
86 | public static RequestCapture parse(HttpResponse response) {
87 | assertEquals(200, response.getStatus());
88 | return read(response, RequestCapture.class);
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/unirest/src/test/java/BehaviorTests/CallbackFutureTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package BehaviorTests;
27 |
28 | import unirest.Unirest;
29 | import unirest.NoopCallback;
30 | import org.junit.Test;
31 |
32 | import static unirest.MockCallback.json;
33 |
34 | public class CallbackFutureTest extends BddTest {
35 |
36 | @Test(timeout = 5000)
37 | public void onSuccess() throws Exception {
38 | Unirest.get(MockServer.GET)
39 | .queryString("Snazzy", "Shoes")
40 | .asJsonAsync()
41 | .thenAccept(r -> {
42 | parse(r).assertParam("Snazzy", "Shoes");
43 | asyncSuccess();
44 | }).get();
45 |
46 | assertAsync();
47 | }
48 |
49 | @Test(timeout = 5000)
50 | public void onSuccessSupplyCallback() throws Exception {
51 | Unirest.get(MockServer.GET)
52 | .queryString("Snazzy", "Shoes")
53 | .asJsonAsync(new NoopCallback<>())
54 | .thenAccept(r -> {
55 | parse(r).assertParam("Snazzy", "Shoes");
56 | asyncSuccess();
57 | }).get();
58 |
59 | assertAsync();
60 | }
61 |
62 | @Test(timeout = 5000)
63 | public void onFailure() throws Exception {
64 | Unirest.get("http://localhost:0000")
65 | .asJsonAsync(json(this))
66 | .isCompletedExceptionally();
67 |
68 | assertFailed("java.net.ConnectException: Connection refused");
69 | }
70 | }
--------------------------------------------------------------------------------
/unirest/src/test/java/BehaviorTests/ConsumerTests.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package BehaviorTests;
27 |
28 | import org.junit.After;
29 | import org.junit.Test;
30 | import unirest.Unirest;
31 |
32 | import static org.junit.Assert.assertEquals;
33 |
34 | public class ConsumerTests extends BddTest {
35 |
36 | private int status;
37 |
38 | @Override
39 | @After
40 | public void tearDown() {
41 | super.tearDown();
42 | status = 0;
43 | }
44 |
45 | @Test
46 | public void canSimplyConsumeAResponse() {
47 | Unirest.get(MockServer.GET)
48 | .thenConsume(r -> status = r.getStatus());
49 |
50 | assertEquals(200, status);
51 | }
52 |
53 | @Test
54 | public void canSimplyConsumeAResponseAsync() {
55 | Unirest.get(MockServer.GET)
56 | .thenConsumeAsync(r -> status = r.getStatus());
57 |
58 | long time = System.currentTimeMillis();
59 | while(System.currentTimeMillis() - time < 5000){
60 | if(status != 0){
61 | break;
62 | }
63 | }
64 | assertEquals(200, status);
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/unirest/src/test/java/BehaviorTests/DefectTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package BehaviorTests;
27 |
28 | import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
29 | import org.apache.http.nio.client.HttpAsyncClient;
30 | import org.json.JSONObject;
31 | import org.junit.Ignore;
32 | import unirest.Unirest;
33 | import org.junit.Test;
34 |
35 | import java.io.IOException;
36 | import java.util.HashMap;
37 | import java.util.Map;
38 | import java.util.stream.IntStream;
39 |
40 | import static junit.framework.TestCase.assertNotSame;
41 | import static junit.framework.TestCase.assertSame;
42 | import static org.junit.Assert.assertEquals;
43 |
44 | public class DefectTest extends BddTest {
45 |
46 | @Test
47 | public void hashOnLinksDoNotMessUpUri() {
48 | Unirest.get(MockServer.GET + "?a=1&b=2#some_location")
49 | .asObject(RequestCapture.class)
50 | .getBody()
51 | .assertParam("a", "1")
52 | .assertParam("b", "2");
53 | }
54 |
55 | @Test
56 | public void nullAndObjectValuesInMap() {
57 | Map queryParams = new HashMap<>();
58 | queryParams.put("foo", null);
59 | queryParams.put("baz", "qux");
60 |
61 | Unirest.get(MockServer.GET)
62 | .queryString(queryParams)
63 | .asObject(RequestCapture.class)
64 | .getBody()
65 | .assertParam("foo", "")
66 | .assertParam("baz", "qux")
67 | .assertQueryString("foo&baz=qux");
68 | }
69 |
70 |
71 | @Test
72 | public void issue_41_IllegalThreadStateExceptionUnderHighLoad() throws IOException {
73 | Unirest.get(MockServer.GET).asStringAsync();
74 |
75 | HttpAsyncClient first = Unirest.config().getAsyncClient().getClient();
76 | IntStream.range(1, 50).forEach(i ->{
77 | assertSame(first, Unirest.config().getAsyncClient().getClient());
78 | });
79 |
80 | ((CloseableHttpAsyncClient)Unirest.config().getAsyncClient().getClient()).close();
81 | Unirest.get(MockServer.GET).asStringAsync();
82 |
83 | HttpAsyncClient second = Unirest.config().getAsyncClient().getClient();
84 | assertNotSame(first, second);
85 |
86 | IntStream.range(1, 50).forEach(i ->{
87 | assertSame(second, Unirest.config().getAsyncClient().getClient());
88 | });
89 | }
90 |
91 | @Test @Ignore
92 | public void trySomeoneElsesGZip() throws Exception {
93 | JSONObject body = Unirest.get("http://httpbin.org/gzip")
94 | .asJsonAsync().get().getBody().getObject();
95 |
96 | assertEquals(true, body.getBoolean("gzipped"));
97 | assertEquals("gzip", body.getJSONObject("headers").getString("Accept-Encoding"));
98 | }
99 | }
100 |
--------------------------------------------------------------------------------
/unirest/src/test/java/BehaviorTests/Foo.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package BehaviorTests;
27 |
28 | import com.google.common.base.MoreObjects;
29 |
30 | public class Foo {
31 | public String bar;
32 |
33 | public Foo(){ }
34 |
35 | public Foo(String bar) {
36 | this.bar = bar;
37 | }
38 |
39 | @Override
40 | public String toString() {
41 | return MoreObjects.toStringHelper(this).add("bar",bar).toString();
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/unirest/src/test/java/BehaviorTests/GZipTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package BehaviorTests;
27 |
28 | import unirest.Unirest;
29 | import org.junit.Test;
30 |
31 | public class GZipTest extends BddTest {
32 | @Test
33 | public void testGzip() {
34 | Unirest.get(MockServer.GZIP)
35 | .queryString("zipme", "up")
36 | .asObject(RequestCapture.class)
37 | .getBody()
38 | .assertParam("zipme", "up");
39 | }
40 |
41 | @Test
42 | public void testGzipAsync() throws Exception {
43 | Unirest.get(MockServer.GZIP)
44 | .queryString("zipme", "up")
45 | .asObjectAsync(RequestCapture.class)
46 | .get()
47 | .getBody()
48 | .assertParam("zipme", "up");
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/unirest/src/test/java/BehaviorTests/GetResponse.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package BehaviorTests;
27 |
28 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
29 | import com.fasterxml.jackson.annotation.JsonProperty;
30 |
31 | @JsonIgnoreProperties(ignoreUnknown = true)
32 | public class GetResponse {
33 |
34 | @JsonProperty("url")
35 | private String url;
36 |
37 | public String getUrl() {
38 | return url;
39 | }
40 |
41 | public void setUrl(String url) {
42 | this.url = url;
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/unirest/src/test/java/BehaviorTests/InterceptorTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package BehaviorTests;
27 |
28 | import unirest.Unirest;
29 | import org.apache.http.HttpException;
30 | import org.apache.http.HttpRequest;
31 | import org.apache.http.HttpRequestInterceptor;
32 | import org.apache.http.protocol.HttpContext;
33 | import org.junit.Test;
34 |
35 | import java.io.IOException;
36 | import java.util.concurrent.ExecutionException;
37 |
38 | public class InterceptorTest extends BddTest {
39 | @Test
40 | public void canAddInterceptor() {
41 | Unirest.config().addInterceptor(new TestInterceptor());
42 |
43 | Unirest.get(MockServer.GET)
44 | .asObject(RequestCapture.class)
45 | .getBody()
46 | .assertHeader("x-custom", "foo");
47 | }
48 |
49 | @Test
50 | public void canAddInterceptorToAsync() throws ExecutionException, InterruptedException {
51 | Unirest.config().addInterceptor(new TestInterceptor());
52 |
53 | Unirest.get(MockServer.GET)
54 | .asObjectAsync(RequestCapture.class)
55 | .get()
56 | .getBody()
57 | .assertHeader("x-custom", "foo");
58 | }
59 |
60 | private class TestInterceptor implements HttpRequestInterceptor {
61 | @Override
62 | public void process(HttpRequest httpRequest, HttpContext httpContext) throws HttpException, IOException {
63 | httpRequest.addHeader("x-custom", "foo");
64 | }
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/unirest/src/test/java/BehaviorTests/ObjectFunctionalTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package BehaviorTests;
27 |
28 | import com.google.common.collect.ImmutableMap;
29 | import com.google.gson.Gson;
30 | import org.junit.Test;
31 | import unirest.Unirest;
32 |
33 | import java.io.InputStreamReader;
34 | import java.util.HashMap;
35 | import java.util.Map;
36 | import java.util.concurrent.ExecutionException;
37 |
38 | import static com.google.common.collect.ImmutableMap.of;
39 | import static org.junit.Assert.assertEquals;
40 |
41 | public class ObjectFunctionalTest extends BddTest {
42 | private Gson gson = new Gson();
43 |
44 | @Test
45 | public void canUseAFunctionToTransform() {
46 | MockServer.setJsonAsResponse(of("foo", "bar"));
47 |
48 | Map r = Unirest.get(MockServer.GET)
49 | .queryString("foo", "bar")
50 | .asObject(i -> gson.fromJson(i.getContentReader(), HashMap.class))
51 | .getBody();
52 |
53 | assertEquals("bar", r.get("foo"));
54 | }
55 |
56 | @Test
57 | public void canUseAFunctionToTransformAsync() throws Exception {
58 | MockServer.setJsonAsResponse(of("foo", "bar"));
59 |
60 | Map r = Unirest.get(MockServer.GET)
61 | .queryString("foo", "bar")
62 | .asObjectAsync(i -> gson.fromJson(i.getContentReader(), HashMap.class))
63 | .get()
64 | .getBody();
65 |
66 | assertEquals("bar", r.get("foo"));
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/unirest/src/test/java/BehaviorTests/PagingTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package BehaviorTests;
27 |
28 | import org.junit.Test;
29 | import unirest.PagedList;
30 | import unirest.Unirest;
31 |
32 | import static org.junit.Assert.assertEquals;
33 |
34 | public class PagingTest extends BddTest {
35 |
36 | @Test
37 | public void canFollowPaging() {
38 | MockServer.expectedPages(10);
39 |
40 | PagedList result = Unirest.get(MockServer.PAGED)
41 | .asPaged(
42 | r -> r.asObject(RequestCapture.class),
43 | r -> r.getHeaders().getFirst("nextPage")
44 | );
45 |
46 | assertEquals(10, result.size());
47 | }
48 |
49 | @Test
50 | public void willReturnOnePageIfthereWasNoPaging() {
51 |
52 | PagedList result = Unirest.get(MockServer.PAGED)
53 | .asPaged(
54 | r -> r.asObject(RequestCapture.class),
55 | r -> null
56 | );
57 |
58 | assertEquals(1, result.size());
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/unirest/src/test/java/BehaviorTests/Pair.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package BehaviorTests;
27 |
28 | public class Pair {
29 | public final K key;
30 | public final V value;
31 |
32 | public Pair(K key, V value){
33 |
34 | this.key = key;
35 | this.value = value;
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/unirest/src/test/java/BehaviorTests/PathParamTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package BehaviorTests;
27 |
28 | import unirest.Unirest;
29 | import unirest.UnirestException;
30 | import org.junit.Test;
31 | import unirest.TestUtil;
32 |
33 | public class PathParamTest extends BddTest {
34 |
35 | @Test
36 | public void testPathParameters() {
37 | Unirest.get(MockServer.HOST + "/{method}")
38 | .routeParam("method", "get")
39 | .queryString("name", "Mark")
40 | .asObject(RequestCapture.class)
41 | .getBody()
42 | .assertParam("name", "Mark");
43 | }
44 |
45 | @Test
46 | public void testQueryAndBodyParameters() {
47 | Unirest.post(MockServer.HOST + "/{method}")
48 | .routeParam("method", "post")
49 | .queryString("name", "Mark")
50 | .field("wot", "wat")
51 | .asObject(RequestCapture.class)
52 | .getBody()
53 | .assertParam("name", "Mark")
54 | .assertParam("wot", "wat");
55 | }
56 |
57 | @Test
58 | public void testPathParameters2() {
59 | Unirest.patch(MockServer.HOST + "/{method}")
60 | .routeParam("method", "patch")
61 | .field("name", "Mark")
62 | .asObject(RequestCapture.class)
63 | .getBody()
64 | .assertParam("name", "Mark");
65 | }
66 |
67 | @Test
68 | public void testMissingPathParameter() {
69 | TestUtil.assertException(() ->
70 | Unirest.get(MockServer.HOST + "/{method}")
71 | .routeParam("method222", "get")
72 | .queryString("name", "Mark")
73 | .asBinary(),
74 | UnirestException.class,
75 | "Can't find route parameter name \"method222\"");
76 | }
77 |
78 | @Test
79 | public void testMissingPathParameterValue() {
80 | TestUtil.assertException(() ->
81 | Unirest.get(MockServer.HOST + "/{method}")
82 | .queryString("name", "Mark")
83 | .asBinary(),
84 | UnirestException.class,
85 | "java.lang.IllegalArgumentException: Illegal character in path at index 22: http://localhost:4567/{method}?name=Mark");
86 | }
87 |
88 | @Test
89 | public void illigalPathParams() {
90 | String value = "/?ЊЯЯ";
91 |
92 | Unirest.get(MockServer.PASSED_PATH_PARAM)
93 | .routeParam("param", value)
94 | .asObject(RequestCapture.class)
95 | .getBody()
96 | .assertUrl("http://localhost:4567/get/%2F%3F%D0%8A%D0%AF%D0%AF/passed")
97 | .assertPathParam(value);
98 | }
99 | }
100 |
--------------------------------------------------------------------------------
/unirest/src/test/java/BehaviorTests/PostRequestHandlersTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package BehaviorTests;
27 |
28 | import org.junit.After;
29 | import org.junit.Test;
30 | import unirest.HttpResponse;
31 | import unirest.Unirest;
32 |
33 | import static org.junit.Assert.*;
34 |
35 | public class PostRequestHandlersTest extends BddTest {
36 |
37 | private HttpResponse captured;
38 |
39 | @Override
40 | @After
41 | public void tearDown() {
42 | super.tearDown();
43 | captured = null;
44 | }
45 |
46 | @Test
47 | public void onSuccessDoSomething() {
48 | Unirest.get(MockServer.GET)
49 | .queryString("foo", "bar")
50 | .asObject(RequestCapture.class)
51 | .ifSuccess(r -> captured = r)
52 | .ifFailure(r -> fail("should not have been called"));
53 |
54 | assertNotNull(captured);
55 | captured.getBody().assertParam("foo", "bar");
56 | }
57 |
58 | @Test
59 | public void onFailDoSomething() {
60 | Unirest.get(MockServer.INVALID_REQUEST)
61 | .queryString("foo", "bar")
62 | .asObject(RequestCapture.class)
63 | .ifFailure(r -> captured = r)
64 | .ifSuccess(r -> fail("should not have been called"));
65 |
66 | assertNotNull(captured);
67 | assertEquals(400, captured.getStatus());
68 | }
69 |
70 | @Test
71 | public void itsAFailIfTheMapperFails() {
72 | MockServer.setStringResponse("not what you expect");
73 |
74 | Unirest.get(MockServer.GET)
75 | .queryString("foo", "bar")
76 | .asObject(RequestCapture.class)
77 | .ifFailure(r -> captured = r)
78 | .ifSuccess(r -> fail("should not have been called"));
79 |
80 | assertNotNull(captured);
81 | assertEquals(200, captured.getStatus());
82 | assertTrue(captured.getParsingError().isPresent());
83 | assertEquals("not what you expect", captured.getParsingError().get().getOriginalBody());
84 | }
85 |
86 |
87 |
88 | @Test
89 | public void onSuccessBeSuccessful() {
90 | HttpResponse response = Unirest.get(MockServer.GET)
91 | .queryString("foo", "bar")
92 | .asObject(RequestCapture.class);
93 |
94 | assertTrue(response.isSuccess());
95 | }
96 |
97 | @Test
98 | public void onFailBeUnsuccessful() {
99 | HttpResponse response = Unirest.get(MockServer.INVALID_REQUEST)
100 | .queryString("foo", "bar")
101 | .asObject(RequestCapture.class);
102 |
103 | assertFalse(response.isSuccess());
104 | }
105 |
106 | @Test
107 | public void beUnsuccessfulIfTheMapperFails() {
108 | MockServer.setStringResponse("not what you expect");
109 |
110 | HttpResponse response = Unirest.get(MockServer.GET)
111 | .queryString("foo", "bar")
112 | .asObject(RequestCapture.class);
113 |
114 | assertFalse(response.isSuccess());
115 | }
116 | }
117 |
--------------------------------------------------------------------------------
/unirest/src/test/java/BehaviorTests/ProxyTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package BehaviorTests;
27 |
28 | import org.apache.http.HttpHost;
29 | import org.junit.After;
30 | import org.junit.Ignore;
31 | import org.junit.Test;
32 | import unirest.HttpResponse;
33 | import unirest.Unirest;
34 |
35 | import static org.junit.Assert.assertTrue;
36 |
37 | @Ignore
38 | public class ProxyTest extends BddTest {
39 |
40 | @After @Override
41 | public void tearDown() {
42 | super.tearDown();
43 | Unirest.shutDown(true);
44 | JankyProxy.shutdown();
45 | }
46 |
47 | @Test
48 | public void canUseNonAuthProxy() {
49 | JankyProxy.runServer("localhost", 4567, 7777);
50 |
51 | Unirest.config().proxy(new HttpHost("localhost", 7777));
52 |
53 | Unirest.get(MockServer.GET)
54 | .asObject(RequestCapture.class)
55 | .getBody()
56 | .assertStatus(200);
57 |
58 | assertTrue(JankyProxy.wasUsed());
59 | }
60 |
61 | @Test
62 | public void canUseNonAuthProxyWithEasyMethod() {
63 | JankyProxy.runServer("localhost", 4567, 7777);
64 |
65 | Unirest.config().proxy("localhost", 7777);
66 |
67 | Unirest.get(MockServer.GET)
68 | .asObject(RequestCapture.class)
69 | .getBody()
70 | .assertStatus(200);
71 |
72 | assertTrue(JankyProxy.wasUsed());
73 | }
74 |
75 | @Test
76 | public void canSetAuthenticatedProxy(){
77 | JankyProxy.runServer("localhost", 4567, 7777);
78 |
79 | Unirest.config().proxy("localhost", 7777, "username", "password1!");
80 |
81 | Unirest.get(MockServer.GET)
82 | .asObject(RequestCapture.class)
83 | .getBody()
84 | .assertStatus(200);
85 |
86 | assertTrue(JankyProxy.wasUsed());
87 | }
88 |
89 | @Test
90 | @Ignore // there is some weird conflict between jetty and unirest here
91 | public void canFlagTheClientsToUseSystemProperties(){
92 | JankyProxy.runServer("localhost", 4567, 7777);
93 |
94 | System.setProperty("http.proxyHost", "localhost");
95 | System.setProperty("http.proxyPort", "7777");
96 |
97 | Unirest.config().useSystemProperties(true);
98 |
99 | Unirest.get(MockServer.GET)
100 | .asObject(RequestCapture.class)
101 | .getBody()
102 | .assertStatus(200);
103 |
104 | assertTrue(JankyProxy.wasUsed());
105 | }
106 |
107 | @Test @Ignore
108 | public void callSomethingRealThroughARealProxy() {
109 | Unirest.config().proxy("proxyv.local.com",81, "myuser","pass1!");
110 | HttpResponse r = Unirest.get("https://twitter.com/ryber").asString();
111 | System.out.println("status = " + r.getStatus());
112 | System.out.println("body= " + r.getBody());
113 | }
114 | }
115 |
--------------------------------------------------------------------------------
/unirest/src/test/java/BehaviorTests/RedirectHandling.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package BehaviorTests;
27 |
28 | import unirest.HttpResponse;
29 | import unirest.Unirest;
30 | import org.junit.Test;
31 |
32 | import java.util.concurrent.ExecutionException;
33 |
34 | import static org.junit.Assert.assertEquals;
35 |
36 | public class RedirectHandling extends BddTest {
37 |
38 | @Test
39 | public void willFollowRedirectsByDefault() {
40 | Unirest.get(MockServer.REDIRECT)
41 | .asObject(RequestCapture.class)
42 | .getBody()
43 | .assertUrl("http://localhost:4567/get");
44 | }
45 |
46 | @Test
47 | public void canDisableRedirects(){
48 | Unirest.config().followRedirects(false);
49 | HttpResponse response = Unirest.get(MockServer.REDIRECT).asBinary();
50 |
51 | assertEquals(301, response.getStatus());
52 | }
53 |
54 | @Test
55 | public void willFollowRedirectsByDefaultAsync() throws ExecutionException, InterruptedException {
56 | Unirest.get(MockServer.REDIRECT)
57 | .asObjectAsync(RequestCapture.class)
58 | .get()
59 | .getBody()
60 | .assertUrl("http://localhost:4567/get");
61 | }
62 |
63 | @Test
64 | public void canDisableRedirectsAsync() throws ExecutionException, InterruptedException {
65 | Unirest.config().followRedirects(false);
66 | HttpResponse response = Unirest.get(MockServer.REDIRECT).asBinaryAsync().get();
67 |
68 | assertEquals(301, response.getStatus());
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/unirest/src/test/java/BehaviorTests/ResponseHeaderTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package BehaviorTests;
27 |
28 | import org.junit.Test;
29 | import unirest.Header;
30 | import unirest.Headers;
31 | import unirest.Unirest;
32 |
33 | import static org.junit.Assert.assertEquals;
34 |
35 | public class ResponseHeaderTest extends BddTest {
36 |
37 |
38 | @Test
39 | public void responseHeadersAreInTheSameOrderAsTheResponse() {
40 | MockServer.addResponseHeader("zed", "oranges");
41 | MockServer.addResponseHeader("alpha", "apples");
42 | MockServer.addResponseHeader("Content", "application/xml");
43 | MockServer.addResponseHeader("zed", "grapes");
44 |
45 | Headers h = Unirest.get(MockServer.GET).asString().getHeaders();
46 |
47 | // assertHeader("Date", "Fri, 04 Jan 2019 01:46:34 GMT", h.all().get(0));
48 | assertHeader("Set-Cookie", "JSESSIONID=ABC123", h.all().get(1));
49 | assertHeader("Expires", "Thu, 01 Jan 1970 00:00:00 GMT", h.all().get(2));
50 | assertHeader("zed", "oranges", h.all().get(3));
51 | assertHeader("alpha", "apples", h.all().get(4));
52 | assertHeader("Content", "application/xml", h.all().get(5));
53 | assertHeader("zed", "grapes", h.all().get(6));
54 | assertHeader("Content-Type", "text/html;charset=utf-8", h.all().get(7));
55 | assertHeader("Transfer-Encoding", "chunked", h.all().get(8));
56 | assertHeader("Server", "Jetty(9.4.12.v20180830)", h.all().get(9));
57 |
58 | }
59 |
60 | private void assertHeader(String name, String value, Header header) {
61 | assertEquals(name, header.getName());
62 | assertEquals(value, header.getValue());
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/unirest/src/test/java/BehaviorTests/VerbTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package BehaviorTests;
27 |
28 | import unirest.HttpMethod;
29 | import unirest.HttpResponse;
30 | import unirest.Unirest;
31 | import org.junit.Test;
32 |
33 | import java.io.InputStream;
34 |
35 | import static org.junit.Assert.assertEquals;
36 |
37 | public class VerbTest extends BddTest {
38 | @Test
39 | public void get() {
40 | Unirest.get(MockServer.GET)
41 | .asObject(RequestCapture.class)
42 | .getBody()
43 | .asserMethod(HttpMethod.GET);
44 | }
45 |
46 | @Test
47 | public void post() {
48 | Unirest.post(MockServer.POST)
49 | .asObject(RequestCapture.class)
50 | .getBody()
51 | .asserMethod(HttpMethod.POST);
52 | }
53 |
54 | @Test
55 | public void put() {
56 | Unirest.put(MockServer.POST)
57 | .asObject(RequestCapture.class)
58 | .getBody()
59 | .asserMethod(HttpMethod.PUT);
60 | }
61 |
62 | @Test
63 | public void patch() {
64 | Unirest.patch(MockServer.PATCH)
65 | .asObject(RequestCapture.class)
66 | .getBody()
67 | .asserMethod(HttpMethod.PATCH);
68 | }
69 |
70 | @Test
71 | public void head() {
72 | HttpResponse response = Unirest.head(MockServer.GET).asBinary();
73 |
74 | assertEquals(200, response.getStatus());
75 | assertEquals("text/html;charset=utf-8", response.getHeaders().getFirst("Content-Type"));
76 | }
77 |
78 | @Test
79 | public void option() {
80 | Unirest.options(MockServer.GET)
81 | .asObject(RequestCapture.class)
82 | .getBody()
83 | .asserMethod(HttpMethod.OPTIONS);
84 | }
85 |
86 | @Test
87 | public void weirdVerbs() {
88 | Unirest.request("CHEESE", MockServer.CHEESE)
89 | .asObject(RequestCapture.class)
90 | .getBody()
91 | .asserMethod(HttpMethod.valueOf("CHEESE"))
92 | .assertBody("");
93 | }
94 | }
95 |
--------------------------------------------------------------------------------
/unirest/src/test/java/unirest/ClientFactoryTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import org.apache.http.HttpRequestInterceptor;
29 | import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
30 | import org.junit.After;
31 | import org.junit.Before;
32 | import org.junit.Test;
33 |
34 | import java.lang.management.ManagementFactory;
35 |
36 | import static org.hamcrest.CoreMatchers.is;
37 | import static org.hamcrest.Matchers.lessThan;
38 | import static org.junit.Assert.*;
39 | import static org.mockito.Mockito.mock;
40 |
41 | public class ClientFactoryTest {
42 |
43 | @Before @After
44 | public void before(){
45 | Unirest.shutDown(true);
46 | }
47 |
48 | @Test
49 | public void shouldReuseThreadPool() {
50 | int startingCount = ManagementFactory.getThreadMXBean().getThreadCount();
51 | //IntStream.range(0,100).forEach(i -> ClientFactory.refresh());
52 | assertThat(ManagementFactory.getThreadMXBean().getThreadCount(), is(lessThan(startingCount + 10)));
53 | }
54 |
55 | @Test
56 | public void canSaveSomeOptions(){
57 | HttpRequestInterceptor i = mock(HttpRequestInterceptor.class);
58 | CloseableHttpAsyncClient c = mock(CloseableHttpAsyncClient.class);
59 |
60 | Unirest.config()
61 | .addInterceptor(i)
62 | .connectTimeout(4000)
63 | .asyncClient(c);
64 |
65 | Unirest.shutDown(false);
66 |
67 | assertNotEquals(c, Unirest.config().getAsyncClient());
68 | assertEquals(i, Unirest.config().getInterceptors().get(0));
69 | assertEquals(4000, Unirest.config().getConnectionTimeout());
70 | }
71 | }
--------------------------------------------------------------------------------
/unirest/src/test/java/unirest/GsonObjectMapper.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import com.google.gson.Gson;
29 | import unirest.GenericType;
30 | import unirest.ObjectMapper;
31 |
32 | public class GsonObjectMapper implements ObjectMapper {
33 |
34 | private final Gson gson = new Gson();
35 |
36 | @Override
37 | public T readValue(String value, Class valueType) {
38 | return gson.fromJson(value, valueType);
39 | }
40 |
41 | @Override
42 | public T readValue(String value, GenericType genericType) {
43 | return gson.fromJson(value, genericType.getType());
44 | }
45 |
46 | @Override
47 | public String writeValue(Object value) {
48 | return gson.toJson(value);
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/unirest/src/test/java/unirest/HeadersTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import org.junit.Test;
29 |
30 | import static org.junit.Assert.assertEquals;
31 |
32 | public class HeadersTest {
33 |
34 | @Test
35 | public void canGetApacheHeaders() {
36 | Headers headers = new Headers();
37 | headers.add("foo","bar");
38 |
39 | Header h = headers.all().get(0);
40 |
41 | assertEquals("foo", h.getName());
42 | assertEquals("bar", h.getValue());
43 | }
44 |
45 | @Test
46 | public void dontBombOnNull(){
47 | Headers h = new Headers();
48 | h.add(null, "foo");
49 |
50 | assertEquals(0, h.size());
51 | }
52 | }
--------------------------------------------------------------------------------
/unirest/src/test/java/unirest/JacksonObjectMapperTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import BehaviorTests.RequestCapture;
29 | import org.junit.Test;
30 | import org.skyscreamer.jsonassert.JSONAssert;
31 |
32 | public class JacksonObjectMapperTest {
33 |
34 | private JacksonObjectMapper om = new JacksonObjectMapper();
35 |
36 | @Test
37 | public void jsonPatch() {
38 | JsonPatch patch = new JsonPatch();
39 | patch.add("/foo", "bar");
40 | patch.add("/baz", "qux");
41 |
42 | String expectStr = patch.toString();
43 | String actualStr = om.writeValue(patch);
44 |
45 | JSONAssert.assertEquals(expectStr,
46 | actualStr,
47 | true);
48 | }
49 |
50 | @Test
51 | public void jsonPatchInRequestCapture() {
52 | JsonPatch patch = new JsonPatch();
53 | patch.add("/foo", "bar");
54 | patch.add("/baz", "qux");
55 |
56 | RequestCapture rc = new RequestCapture();
57 | rc.setPatch(patch);
58 |
59 | String actualStr = om.writeValue(rc);
60 | System.out.println("actualStr = " + actualStr);
61 | JSONAssert.assertEquals("{}",
62 | actualStr,
63 | false);
64 | }
65 | }
--------------------------------------------------------------------------------
/unirest/src/test/java/unirest/JsonNodeTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import org.junit.Test;
29 | import unirest.JsonNode;
30 |
31 | import static org.junit.Assert.*;
32 |
33 | public class JsonNodeTest {
34 |
35 | @Test
36 | public void canParseARegularObject() {
37 | String json = "{\"foo\":\"bar\"}";
38 | JsonNode node = new JsonNode(json);
39 | assertEquals(false, node.isArray());
40 | assertEquals("bar", node.getObject().getString("foo"));
41 | assertEquals("bar", node.getArray().getJSONObject(0).getString("foo"));
42 | assertEquals(json, node.toString());
43 | }
44 |
45 | @Test
46 | public void canParseArrayObject() {
47 | String json = "[{\"foo\":\"bar\"}]";
48 | JsonNode node = new JsonNode(json);
49 | assertEquals(true, node.isArray());
50 | assertEquals("bar", node.getArray().getJSONObject(0).getString("foo"));
51 | assertEquals(null, node.getObject());
52 | assertEquals(json, node.toString());
53 | }
54 |
55 | @Test
56 | public void nullAndEmptyObjectsResultInEmptyJson() {
57 | assertEquals("{}", new JsonNode("").toString());
58 | assertEquals("{}", new JsonNode(null).toString());
59 | }
60 | }
--------------------------------------------------------------------------------
/unirest/src/test/java/unirest/MockCallback.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import BehaviorTests.BddTest;
29 |
30 | import java.util.function.Consumer;
31 |
32 | public class MockCallback implements Callback {
33 |
34 | public static MockCallback json(BddTest test){
35 | return new MockCallback<>(test);
36 | }
37 |
38 | private BddTest test;
39 | private Consumer> onSuccess = r -> {};
40 | private Consumer onFail = f -> {};
41 | private Runnable onCancel = () -> {};
42 |
43 | public MockCallback(BddTest test){
44 | this.test = test;
45 | }
46 |
47 | public MockCallback(BddTest test, Consumer> onSuccess){
48 | this.test = test;
49 | this.onSuccess = onSuccess;
50 | }
51 |
52 | public MockCallback onFail(Consumer onFail){
53 | this.onFail = onFail;
54 | return this;
55 | }
56 |
57 | public MockCallback onCancel(Consumer onFail){
58 | this.onFail = onFail;
59 | return this;
60 | }
61 |
62 | @Override
63 | public void completed(HttpResponse response) {
64 | onSuccess.accept(response);
65 | test.asyncSuccess();
66 | }
67 |
68 | @Override
69 | public void failed(UnirestException e) {
70 | test.asyncFail(e.getMessage());
71 | }
72 |
73 | @Override
74 | public void cancelled() {
75 | test.asyncFail("canceled");
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/unirest/src/test/java/unirest/NoopCallback.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import unirest.HttpResponse;
29 | import unirest.Callback;
30 | import unirest.UnirestException;
31 |
32 | public class NoopCallback implements Callback {
33 | @Override
34 | public void completed(HttpResponse response) {
35 |
36 | }
37 |
38 | @Override
39 | public void failed(UnirestException e) {
40 |
41 | }
42 |
43 | @Override
44 | public void cancelled() {
45 |
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/unirest/src/test/java/unirest/NotImplimented.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | public class NotImplimented extends RuntimeException {
29 | }
30 |
--------------------------------------------------------------------------------
/unirest/src/test/java/unirest/PagedListTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import com.google.common.base.Strings;
29 | import org.junit.Test;
30 |
31 | import java.util.ArrayList;
32 | import java.util.Arrays;
33 | import java.util.List;
34 |
35 | import static java.util.Arrays.asList;
36 | import static org.junit.Assert.assertEquals;
37 | import static org.mockito.Mockito.mock;
38 | import static org.mockito.Mockito.when;
39 |
40 | public class PagedListTest {
41 |
42 | @Test
43 | public void canGetAllTheBodies() {
44 | PagedList list = new PagedList<>();
45 | list.addAll(asList(
46 | mkRequest("foo"),
47 | mkRequest("bar"),
48 | mkRequest("baz")
49 | ));
50 |
51 | List bodies = list.getBodies();
52 | assertEquals(Arrays.asList("foo","bar","baz"), bodies);
53 | }
54 |
55 | @Test
56 | public void bodiesMustBeSucessful() {
57 | PagedList list = new PagedList<>();
58 | list.addAll(asList(
59 | mkRequest("foo"),
60 | mkRequest(null),
61 | mkRequest("baz")
62 | ));
63 |
64 | List bodies = list.getBodies();
65 | assertEquals(Arrays.asList("foo","baz"), bodies);
66 | }
67 |
68 | @Test
69 | public void canProcessSuccessfullResuls() {
70 | PagedList list = new PagedList<>();
71 | list.addAll(asList(
72 | mkRequest("foo"),
73 | mkRequest(null),
74 | mkRequest("baz")
75 | ));
76 |
77 | final List processed = new ArrayList<>();
78 | list.ifSuccess(e -> processed.add(e.getBody()));
79 | assertEquals(Arrays.asList("foo","baz"), processed);
80 | }
81 |
82 | @Test
83 | public void canProcessFailed() {
84 | PagedList list = new PagedList<>();
85 | list.addAll(asList(
86 | mkRequest("foo"),
87 | mkRequest(null),
88 | mkRequest("baz")
89 | ));
90 |
91 | final List processed = new ArrayList<>();
92 | list.ifFailure(e -> processed.add(e.getBody()));
93 | assertEquals(null, processed.get(0));
94 | }
95 |
96 | private HttpResponse mkRequest(String foo) {
97 | HttpResponse r = mock(HttpResponse.class);
98 | when(r.getBody()).thenReturn(foo);
99 | when(r.isSuccess()).thenReturn(!Strings.isNullOrEmpty(foo));
100 | return r;
101 | }
102 | }
--------------------------------------------------------------------------------
/unirest/src/test/java/unirest/ResponseUtilsTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import org.junit.Test;
29 | import org.junit.runner.RunWith;
30 | import org.mockito.InjectMocks;
31 | import org.mockito.Mock;
32 | import org.mockito.runners.MockitoJUnitRunner;
33 |
34 | import java.io.InputStream;
35 | import java.io.InputStreamReader;
36 |
37 | import static org.junit.Assert.assertEquals;
38 | import static org.mockito.Mockito.mock;
39 | import static org.mockito.Mockito.when;
40 |
41 | @RunWith(MockitoJUnitRunner.class)
42 | public class ResponseUtilsTest {
43 | @Mock
44 | private Config config;
45 | @InjectMocks
46 | TestResponse test;
47 |
48 | @Test
49 | public void getCharsetDefaults() {
50 | defaultEncoding("UTF-8");
51 |
52 | assertEquals("UTF-8", getCharSet(null));
53 | assertEquals("UTF-8", getCharSet(""));
54 | assertEquals("UTF-8", getCharSet(" "));
55 | assertEquals("UTF-8", getCharSet("Content-Type: text/html;"));
56 | assertEquals("UTF-8", getCharSet("Content-Type: text/html; charset="));
57 | }
58 |
59 | @Test
60 | public void contentTypeWhenYouGotIt() {
61 | assertEquals("LATIN-1", getCharSet("Content-Type: text/html; charset=latin-1"));
62 | }
63 |
64 | @Test
65 | public void changeTheDefault() {
66 | defaultEncoding("KINGON-1");
67 | assertEquals("KINGON-1", getCharSet(null));
68 | defaultEncoding("SINDARIN-42");
69 | assertEquals("SINDARIN-42", getCharSet(null));
70 | }
71 |
72 | private void defaultEncoding(String t) {
73 | when(config.getDefaultResponseEncoding()).thenReturn(t);
74 | }
75 |
76 | private String getCharSet(String content) {
77 | test.type = content;
78 | return test.getCharSet();
79 | }
80 |
81 | public static class TestResponse extends RawResponseBase {
82 |
83 | public String type;
84 |
85 | protected TestResponse(Config config) {
86 | super(config);
87 | }
88 |
89 | @Override
90 | public int getStatus() {
91 | return 0;
92 | }
93 |
94 | @Override
95 | public String getStatusText() {
96 | return null;
97 | }
98 |
99 | @Override
100 | public Headers getHeaders() {
101 | return null;
102 | }
103 |
104 | @Override
105 | public InputStream getContent() {
106 | return null;
107 | }
108 |
109 | @Override
110 | public byte[] getContentAsBytes() {
111 | return new byte[0];
112 | }
113 |
114 | @Override
115 | public String getContentAsString() {
116 | return null;
117 | }
118 |
119 | @Override
120 | public String getContentAsString(String charset) {
121 | return null;
122 | }
123 |
124 | @Override
125 | public InputStreamReader getContentReader() {
126 | return null;
127 | }
128 |
129 | @Override
130 | public boolean hasContent() {
131 | return false;
132 | }
133 |
134 | @Override
135 | public String getContentType() {
136 | return type;
137 | }
138 |
139 | @Override
140 | public String getEncoding() {
141 | return null;
142 | }
143 | }
144 | }
--------------------------------------------------------------------------------
/unirest/src/test/java/unirest/UriFormatterTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import org.junit.Test;
29 |
30 | import static org.junit.Assert.assertEquals;
31 |
32 | public class UriFormatterTest {
33 |
34 | @Test
35 | public void testMangler_encoding() {
36 | assertLinkSurvives("http://localhost/test%2Fthis");
37 | }
38 |
39 | @Test
40 | public void testMangler_fragment() {
41 | assertLinkSurvives("http://localhost/test?a=b#fragment");
42 | }
43 |
44 | @Test
45 | public void basicBoringUri() {
46 | assertLinkSurvives("http://localhost/test?a=b");
47 | }
48 |
49 | @Test
50 | public void semicolonsAsParam() {
51 | assertLinkSurvives("http://localhost/test?a=b;foo=bar");
52 | }
53 |
54 | @Test
55 | public void utf8Chars(){
56 | assertLinkSurvives("http://localhost/test?foo=こんにちは");
57 | }
58 |
59 | private void assertLinkSurvives(String s) {
60 | assertEquals(s, new BaseRequest(new Config(), HttpMethod.GET, s){}.url.toString());
61 | }
62 |
63 | }
--------------------------------------------------------------------------------
/unirest/src/test/java/unirest/UtilTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * The MIT License
3 | *
4 | * Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * "Software"), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be
15 | * included in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 | */
25 |
26 | package unirest;
27 |
28 | import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
29 | import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
30 | import org.apache.http.nio.client.HttpAsyncClient;
31 | import org.junit.Test;
32 |
33 | import static org.junit.Assert.*;
34 |
35 | public class UtilTest {
36 |
37 | @Test
38 | public void canCast() {
39 | Object foo = new Foo(){};
40 |
41 | assertEquals(foo, Util.tryCast(foo, Foo.class).get());
42 | assertEquals(false, Util.tryCast("foo", Foo.class).isPresent());
43 | assertEquals(false, Util.tryCast(null, Foo.class).isPresent());
44 | assertEquals(true, Util.tryCast(new Bar(), Foo.class).isPresent());
45 | }
46 |
47 | @Test
48 | public void canCastAAsyncClient() {
49 | HttpAsyncClient build = HttpAsyncClientBuilder.create().build();
50 |
51 | assertEquals(true, Util.tryCast(build, CloseableHttpAsyncClient.class).isPresent());
52 | }
53 |
54 | public abstract class Foo {}
55 |
56 | public class Bar extends Foo {}
57 | }
--------------------------------------------------------------------------------
/unirest/src/test/resources/data/cp1250.txt:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OpenUnirest/unirest-java/c2baad31cbb3b728236748fa5d3d07f89d15b491/unirest/src/test/resources/data/cp1250.txt
--------------------------------------------------------------------------------
/unirest/src/test/resources/image.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OpenUnirest/unirest-java/c2baad31cbb3b728236748fa5d3d07f89d15b491/unirest/src/test/resources/image.jpg
--------------------------------------------------------------------------------
/unirest/src/test/resources/test:
--------------------------------------------------------------------------------
1 | This is a test file
--------------------------------------------------------------------------------
/unirest/src/test/resources/test-json-patch.json:
--------------------------------------------------------------------------------
1 | [
2 | {"op":"add","path":"/fruits/-","value":"Apple"},
3 | {"op":"remove","path":"/bugs"},
4 | {"op":"replace","path":"/lastname","value":"Flintstone"},
5 | {"op":"test","path":"/firstname","value":"Fred"},
6 | {"op":"move","path":"/new/location","from":"/old/location"},
7 | {"op":"copy","path":"/new/location","from":"/original/location"}
8 | ]
--------------------------------------------------------------------------------