Added iri.Equals method
This commit is contained in:
parent
8a9ddc74a4
commit
e6341caa81
58
iri.go
58
iri.go
|
@ -9,7 +9,7 @@ const PublicNS = IRI("https://www.w3.org/ns/activitystreams#Public")
|
|||
|
||||
type (
|
||||
// IRI is a Internationalized Resource Identifiers (IRIs) RFC3987
|
||||
IRI string
|
||||
IRI string
|
||||
IRIs []IRI
|
||||
)
|
||||
|
||||
|
@ -34,7 +34,7 @@ func (i *IRI) UnmarshalJSON(s []byte) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// IsObject
|
||||
// GetID
|
||||
func (i IRI) GetID() *ObjectID {
|
||||
o := ObjectID(i)
|
||||
return &o
|
||||
|
@ -57,14 +57,14 @@ func (i IRI) IsObject() bool {
|
|||
|
||||
// FlattenToIRI checks if Item can be flatten to an IRI and returns it if so
|
||||
func FlattenToIRI(it Item) Item {
|
||||
if it!= nil && it.IsObject() && len(it.GetLink()) > 0 {
|
||||
if it != nil && it.IsObject() && len(it.GetLink()) > 0 {
|
||||
return it.GetLink()
|
||||
}
|
||||
return it
|
||||
}
|
||||
|
||||
// Contains verifies if IRIs array contains the received one
|
||||
func(i IRIs) Contains(r IRI) bool {
|
||||
func (i IRIs) Contains(r IRI) bool {
|
||||
if len(i) == 0 {
|
||||
return false
|
||||
}
|
||||
|
@ -75,3 +75,53 @@ func(i IRIs) Contains(r IRI) bool {
|
|||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Equals verifies if our receiver IRI is equals with the "with" IRI
|
||||
// It ignores the protocol
|
||||
// It tries to use the URL representation if possible and fallback to string comparison if unable to convert
|
||||
// In URL representation checks hostname in a case insensitive way and the path and
|
||||
func (i IRI) Equals(with IRI, checkScheme bool) bool {
|
||||
u, e := i.URL()
|
||||
uw, ew := with.URL()
|
||||
if e != nil || ew != nil {
|
||||
return strings.ToLower(i.String()) == strings.ToLower(with.String())
|
||||
}
|
||||
if checkScheme {
|
||||
if strings.ToLower(u.Scheme) != strings.ToLower(uw.Scheme) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
if strings.ToLower(u.Host) != strings.ToLower(uw.Host) {
|
||||
return false
|
||||
}
|
||||
if u.Path != uw.Path {
|
||||
return false
|
||||
}
|
||||
uq := u.Query()
|
||||
uwq := uw.Query()
|
||||
if len(uq) != len(uwq) {
|
||||
return false
|
||||
}
|
||||
for k, uqv := range uq {
|
||||
uwqv, ok := uwq[k]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
if len(uqv) != len(uwqv) {
|
||||
return false
|
||||
}
|
||||
for _, uqvv := range uqv {
|
||||
eq := false
|
||||
for _, uwqvv := range uwqv {
|
||||
if uwqvv == uqvv {
|
||||
eq = true
|
||||
continue
|
||||
}
|
||||
}
|
||||
if !eq {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
|
140
iri_test.go
140
iri_test.go
|
@ -70,3 +70,143 @@ func TestIRI_URL(t *testing.T) {
|
|||
func TestIRIs_Contains(t *testing.T) {
|
||||
t.Skipf("TODO")
|
||||
}
|
||||
|
||||
func TestIRI_Equals(t *testing.T) {
|
||||
{
|
||||
i1 := IRI("http://example.com")
|
||||
i2 := IRI("http://example.com")
|
||||
// same host same scheme
|
||||
if !i1.Equals(i2, true) {
|
||||
t.Errorf("%s should equal %s", i1, i2)
|
||||
}
|
||||
}
|
||||
{
|
||||
i1 := IRI("http://example.com/ana/are/mere")
|
||||
i2 := IRI("http://example.com/ana/are/mere")
|
||||
// same host, same scheme and same path
|
||||
if !i1.Equals(i2, true) {
|
||||
t.Errorf("%s should equal %s", i1, i2)
|
||||
}
|
||||
}
|
||||
{
|
||||
i1 := IRI("https://example.com")
|
||||
i2 := IRI("http://example.com")
|
||||
// same host different scheme
|
||||
if !i1.Equals(i2, false) {
|
||||
t.Errorf("%s should equal %s", i1, i2)
|
||||
}
|
||||
}
|
||||
{
|
||||
i1 := IRI("http://example.com/ana/are/mere")
|
||||
i2 := IRI("https://example.com/ana/are/mere")
|
||||
// same host, different scheme and same path
|
||||
if !i1.Equals(i2, false) {
|
||||
t.Errorf("%s should equal %s", i1, i2)
|
||||
}
|
||||
}
|
||||
{
|
||||
i1 := IRI("https://example.com?ana=mere")
|
||||
i2 := IRI("http://example.com?ana=mere")
|
||||
// same host different scheme, same query
|
||||
if !i1.Equals(i2, false) {
|
||||
t.Errorf("%s should equal %s", i1, i2)
|
||||
}
|
||||
}
|
||||
{
|
||||
i1 := IRI("https://example.com?ana=mere&foo=bar")
|
||||
i2 := IRI("http://example.com?foo=bar&ana=mere")
|
||||
// same host different scheme, same query - different order
|
||||
if !i1.Equals(i2, false) {
|
||||
t.Errorf("%s should equal %s", i1, i2)
|
||||
}
|
||||
}
|
||||
{
|
||||
i1 := IRI("http://example.com/ana/are/mere?foo=bar&ana=mere")
|
||||
i2 := IRI("https://example.com/ana/are/mere?ana=mere&foo=bar")
|
||||
// same host, different scheme and same path, same query different order
|
||||
if !i1.Equals(i2, false) {
|
||||
t.Errorf("%s should equal %s", i1, i2)
|
||||
}
|
||||
}
|
||||
{
|
||||
i1 := IRI("https://example.com?ana=mere")
|
||||
i2 := IRI("http://example.com?ana=mere")
|
||||
// same host different scheme, same query
|
||||
if !i1.Equals(i2, false) {
|
||||
t.Errorf("%s should equal %s", i1, i2)
|
||||
}
|
||||
}
|
||||
{
|
||||
i1 := IRI("https://example.com?ana=mere&foo=bar")
|
||||
i2 := IRI("http://example.com?foo=bar&ana=mere")
|
||||
// same host different scheme, same query - different order
|
||||
if !i1.Equals(i2, false) {
|
||||
t.Errorf("%s should equal %s", i1, i2)
|
||||
}
|
||||
}
|
||||
{
|
||||
i1 := IRI("http://example.com/ana/are/mere?foo=bar&ana=mere")
|
||||
i2 := IRI("https://example.com/ana/are/mere?ana=mere&foo=bar")
|
||||
// same host, different scheme and same path, same query different order
|
||||
if !i1.Equals(i2, false) {
|
||||
t.Errorf("%s should equal %s", i1, i2)
|
||||
}
|
||||
}
|
||||
///
|
||||
{
|
||||
i1 := IRI("http://example.com")
|
||||
i2 := IRI("https://example.com")
|
||||
// same host different scheme
|
||||
if i1.Equals(i2, true) {
|
||||
t.Errorf("%s should not equal %s", i1, i2)
|
||||
}
|
||||
}
|
||||
{
|
||||
i1 := IRI("http://example1.com")
|
||||
i2 := IRI("http://example.com")
|
||||
// different host same scheme
|
||||
if i1.Equals(i2, true) {
|
||||
t.Errorf("%s should not equal %s", i1, i2)
|
||||
}
|
||||
}
|
||||
{
|
||||
i1 := IRI("http://example.com/ana/1are/mere")
|
||||
i2 := IRI("http://example.com/ana/are/mere")
|
||||
// same host, same scheme and different path
|
||||
if i1.Equals(i2, true) {
|
||||
t.Errorf("%s should not equal %s", i1, i2)
|
||||
}
|
||||
}
|
||||
{
|
||||
i1 := IRI("http://example.com?ana1=mere")
|
||||
i2 := IRI("http://example.com?ana=mere")
|
||||
// same host same scheme, different query key
|
||||
if i1.Equals(i2, false) {
|
||||
t.Errorf("%s should not equal %s", i1, i2)
|
||||
}
|
||||
}
|
||||
{
|
||||
i1 := IRI("http://example.com?ana=mere")
|
||||
i2 := IRI("http://example.com?ana=mere1")
|
||||
// same host same scheme, different query value
|
||||
if i1.Equals(i2, false) {
|
||||
t.Errorf("%s should not equal %s", i1, i2)
|
||||
}
|
||||
}
|
||||
{
|
||||
i1 := IRI("https://example.com?ana=mere&foo=bar")
|
||||
i2 := IRI("http://example.com?foo=bar1&ana=mere")
|
||||
// same host different scheme, different query value - different order
|
||||
if i1.Equals(i2, false) {
|
||||
t.Errorf("%s should not equal %s", i1, i2)
|
||||
}
|
||||
}
|
||||
{
|
||||
i1 := IRI("http://example.com/ana/are/mere?foo=bar&ana=mere")
|
||||
i2 := IRI("https://example.com/ana/are/mere?ana=mere&foo1=bar")
|
||||
// same host, different scheme and same path, differnt query key different order
|
||||
if i1.Equals(i2, false) {
|
||||
t.Errorf("%s should not equal %s", i1, i2)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue