Skip to content

Commit

Permalink
equals on path
Browse files Browse the repository at this point in the history
  • Loading branch information
ryber committed Feb 13, 2020
1 parent a076a72 commit 8872acb
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
14 changes: 14 additions & 0 deletions unirest/src/main/java/kong/unirest/Path.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.net.URLEncoder;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -104,4 +105,17 @@ public String toString() {
public String rawPath() {
return rawPath;
}

@Override
public boolean equals(Object o) {
if (this == o) { return true; }
if (o == null || getClass() != o.getClass()) { return false; }
Path path = (Path) o;
return Objects.equals(url, path.url);
}

@Override
public int hashCode() {
return Objects.hash(url);
}
}
67 changes: 67 additions & 0 deletions unirest/src/test/java/kong/unirest/PathTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* The MIT License
*
* Copyright for portions of unirest-java are held by Kong Inc (c) 2013.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package kong.unirest;

import org.junit.Test;

import static org.junit.Assert.*;

public class PathTest {
@Test
public void pathIsTheSameIfUrlIs() {
Path p = new Path("https://localhost/apples");
Path o = new Path("https://localhost/apples");
assertEquals(p, o);
}

@Test
public void pathsWithDifferentParams() {
String raw = "http://somewhere/fruits/{id}";
Path q = new Path(raw);
q.param("id", "apple");
Path w = new Path(raw);
w.param("id", "apple");
Path e = new Path(raw);
e.param("id", "oranges");

assertEquals(q,w);
assertNotEquals(q, e);
}

@Test
public void queryParamsMatter() {
String raw = "http://somewhere/fruits/}";
Path q = new Path(raw);
q.queryString("id", "apple");
Path w = new Path(raw);
w.queryString("id", "apple");
Path e = new Path(raw);
e.queryString("id", "oranges");

assertEquals(q,w);
assertNotEquals(q, e);
}
}

0 comments on commit 8872acb

Please sign in to comment.