| 1 |
// Copyright 2015 xeipuuv ( https://github.com/xeipuuv ) |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
// author xeipuuv |
| 16 |
// author-github https://github.com/xeipuuv |
| 17 |
// author-mail [email protected] |
| 18 |
// |
| 19 |
// repository-name gojsonreference |
| 20 |
// repository-desc An implementation of JSON Reference - Go language |
| 21 |
// |
| 22 |
// description Automated tests on package. |
| 23 |
// |
| 24 |
// created 03-03-2013 |
| 25 |
|
| 26 |
package gojsonreference |
| 27 |
|
| 28 |
import ( |
| 29 |
"testing" |
| 30 |
) |
| 31 |
|
| 32 |
func TestFull(t *testing.T) { |
| 33 |
|
| 34 |
in := "http://host/path/a/b/c#/f/a/b" |
| 35 |
|
| 36 |
r1, err := NewJsonReference(in) |
| 37 |
if err != nil { |
| 38 |
t.Errorf("NewJsonReference(%v) error %s", in, err.Error()) |
| 39 |
} |
| 40 |
|
| 41 |
if in != r1.String() { |
| 42 |
t.Errorf("NewJsonReference(%v) = %v, expect %v", in, r1.String(), in) |
| 43 |
} |
| 44 |
|
| 45 |
if r1.HasFragmentOnly != false { |
| 46 |
t.Errorf("NewJsonReference(%v)::HasFragmentOnly %v expect %v", in, r1.HasFragmentOnly, false) |
| 47 |
} |
| 48 |
|
| 49 |
if r1.HasFullUrl != true { |
| 50 |
t.Errorf("NewJsonReference(%v)::HasFullUrl %v expect %v", in, r1.HasFullUrl, true) |
| 51 |
} |
| 52 |
|
| 53 |
if r1.HasUrlPathOnly != false { |
| 54 |
t.Errorf("NewJsonReference(%v)::HasUrlPathOnly %v expect %v", in, r1.HasUrlPathOnly, false) |
| 55 |
} |
| 56 |
|
| 57 |
if r1.HasFileScheme != false { |
| 58 |
t.Errorf("NewJsonReference(%v)::HasFileScheme %v expect %v", in, r1.HasFileScheme, false) |
| 59 |
} |
| 60 |
|
| 61 |
if r1.GetPointer().String() != "/f/a/b" { |
| 62 |
t.Errorf("NewJsonReference(%v)::GetPointer() %v expect %v", in, r1.GetPointer().String(), "/f/a/b") |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
func TestFullUrl(t *testing.T) { |
| 67 |
|
| 68 |
in := "http://host/path/a/b/c" |
| 69 |
|
| 70 |
r1, err := NewJsonReference(in) |
| 71 |
if err != nil { |
| 72 |
t.Errorf("NewJsonReference(%v) error %s", in, err.Error()) |
| 73 |
} |
| 74 |
|
| 75 |
if in != r1.String() { |
| 76 |
t.Errorf("NewJsonReference(%v) = %v, expect %v", in, r1.String(), in) |
| 77 |
} |
| 78 |
|
| 79 |
if r1.HasFragmentOnly != false { |
| 80 |
t.Errorf("NewJsonReference(%v)::HasFragmentOnly %v expect %v", in, r1.HasFragmentOnly, false) |
| 81 |
} |
| 82 |
|
| 83 |
if r1.HasFullUrl != true { |
| 84 |
t.Errorf("NewJsonReference(%v)::HasFullUrl %v expect %v", in, r1.HasFullUrl, true) |
| 85 |
} |
| 86 |
|
| 87 |
if r1.HasUrlPathOnly != false { |
| 88 |
t.Errorf("NewJsonReference(%v)::HasUrlPathOnly %v expect %v", in, r1.HasUrlPathOnly, false) |
| 89 |
} |
| 90 |
|
| 91 |
if r1.HasFileScheme != false { |
| 92 |
t.Errorf("NewJsonReference(%v)::HasFileScheme %v expect %v", in, r1.HasFileScheme, false) |
| 93 |
} |
| 94 |
|
| 95 |
if r1.GetPointer().String() != "" { |
| 96 |
t.Errorf("NewJsonReference(%v)::GetPointer() %v expect %v", in, r1.GetPointer().String(), "") |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
func TestFragmentOnly(t *testing.T) { |
| 101 |
|
| 102 |
in := "#/fragment/only" |
| 103 |
|
| 104 |
r1, err := NewJsonReference(in) |
| 105 |
if err != nil { |
| 106 |
t.Errorf("NewJsonReference(%v) error %s", in, err.Error()) |
| 107 |
} |
| 108 |
|
| 109 |
if in != r1.String() { |
| 110 |
t.Errorf("NewJsonReference(%v) = %v, expect %v", in, r1.String(), in) |
| 111 |
} |
| 112 |
|
| 113 |
if r1.HasFragmentOnly != true { |
| 114 |
t.Errorf("NewJsonReference(%v)::HasFragmentOnly %v expect %v", in, r1.HasFragmentOnly, true) |
| 115 |
} |
| 116 |
|
| 117 |
if r1.HasFullUrl != false { |
| 118 |
t.Errorf("NewJsonReference(%v)::HasFullUrl %v expect %v", in, r1.HasFullUrl, false) |
| 119 |
} |
| 120 |
|
| 121 |
if r1.HasUrlPathOnly != false { |
| 122 |
t.Errorf("NewJsonReference(%v)::HasUrlPathOnly %v expect %v", in, r1.HasUrlPathOnly, false) |
| 123 |
} |
| 124 |
|
| 125 |
if r1.HasFileScheme != false { |
| 126 |
t.Errorf("NewJsonReference(%v)::HasFileScheme %v expect %v", in, r1.HasFileScheme, false) |
| 127 |
} |
| 128 |
|
| 129 |
if r1.GetPointer().String() != "/fragment/only" { |
| 130 |
t.Errorf("NewJsonReference(%v)::GetPointer() %v expect %v", in, r1.GetPointer().String(), "/fragment/only") |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
func TestUrlPathOnly(t *testing.T) { |
| 135 |
|
| 136 |
in := "/documents/document.json" |
| 137 |
|
| 138 |
r1, err := NewJsonReference(in) |
| 139 |
if err != nil { |
| 140 |
t.Errorf("NewJsonReference(%v) error %s", in, err.Error()) |
| 141 |
} |
| 142 |
|
| 143 |
if in != r1.String() { |
| 144 |
t.Errorf("NewJsonReference(%v) = %v, expect %v", in, r1.String(), in) |
| 145 |
} |
| 146 |
|
| 147 |
if r1.HasFragmentOnly != false { |
| 148 |
t.Errorf("NewJsonReference(%v)::HasFragmentOnly %v expect %v", in, r1.HasFragmentOnly, false) |
| 149 |
} |
| 150 |
|
| 151 |
if r1.HasFullUrl != false { |
| 152 |
t.Errorf("NewJsonReference(%v)::HasFullUrl %v expect %v", in, r1.HasFullUrl, false) |
| 153 |
} |
| 154 |
|
| 155 |
if r1.HasUrlPathOnly != true { |
| 156 |
t.Errorf("NewJsonReference(%v)::HasUrlPathOnly %v expect %v", in, r1.HasUrlPathOnly, true) |
| 157 |
} |
| 158 |
|
| 159 |
if r1.HasFileScheme != false { |
| 160 |
t.Errorf("NewJsonReference(%v)::HasFileScheme %v expect %v", in, r1.HasFileScheme, false) |
| 161 |
} |
| 162 |
|
| 163 |
if r1.GetPointer().String() != "" { |
| 164 |
t.Errorf("NewJsonReference(%v)::GetPointer() %v expect %v", in, r1.GetPointer().String(), "") |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
func TestUrlRelativePathOnly(t *testing.T) { |
| 169 |
|
| 170 |
in := "document.json" |
| 171 |
|
| 172 |
r1, err := NewJsonReference(in) |
| 173 |
if err != nil { |
| 174 |
t.Errorf("NewJsonReference(%v) error %s", in, err.Error()) |
| 175 |
} |
| 176 |
|
| 177 |
if in != r1.String() { |
| 178 |
t.Errorf("NewJsonReference(%v) = %v, expect %v", in, r1.String(), in) |
| 179 |
} |
| 180 |
|
| 181 |
if r1.HasFragmentOnly != false { |
| 182 |
t.Errorf("NewJsonReference(%v)::HasFragmentOnly %v expect %v", in, r1.HasFragmentOnly, false) |
| 183 |
} |
| 184 |
|
| 185 |
if r1.HasFullUrl != false { |
| 186 |
t.Errorf("NewJsonReference(%v)::HasFullUrl %v expect %v", in, r1.HasFullUrl, false) |
| 187 |
} |
| 188 |
|
| 189 |
if r1.HasUrlPathOnly != true { |
| 190 |
t.Errorf("NewJsonReference(%v)::HasUrlPathOnly %v expect %v", in, r1.HasUrlPathOnly, true) |
| 191 |
} |
| 192 |
|
| 193 |
if r1.HasFileScheme != false { |
| 194 |
t.Errorf("NewJsonReference(%v)::HasFileScheme %v expect %v", in, r1.HasFileScheme, false) |
| 195 |
} |
| 196 |
|
| 197 |
if r1.GetPointer().String() != "" { |
| 198 |
t.Errorf("NewJsonReference(%v)::GetPointer() %v expect %v", in, r1.GetPointer().String(), "") |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
func TestInheritsValid(t *testing.T) { |
| 203 |
|
| 204 |
in1 := "http://www.test.com/doc.json" |
| 205 |
in2 := "#/a/b" |
| 206 |
out := in1 + in2 |
| 207 |
|
| 208 |
r1, _ := NewJsonReference(in1) |
| 209 |
r2, _ := NewJsonReference(in2) |
| 210 |
|
| 211 |
result, err := r1.Inherits(r2) |
| 212 |
if err != nil { |
| 213 |
t.Errorf("Inherits(%s,%s) error %s", r1.String(), r2.String(), err.Error()) |
| 214 |
} |
| 215 |
|
| 216 |
if result.String() != out { |
| 217 |
t.Errorf("Inherits(%s,%s) = %s, expect %s", r1.String(), r2.String(), result.String(), out) |
| 218 |
} |
| 219 |
|
| 220 |
if result.GetPointer().String() != "/a/b" { |
| 221 |
t.Errorf("result(%v)::GetPointer() %v expect %v", result.String(), result.GetPointer().String(), "/a/b") |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
func TestInheritsDifferentHost(t *testing.T) { |
| 226 |
|
| 227 |
in1 := "http://www.test.com/doc.json" |
| 228 |
in2 := "http://www.test2.com/doc.json#bla" |
| 229 |
|
| 230 |
r1, _ := NewJsonReference(in1) |
| 231 |
r2, _ := NewJsonReference(in2) |
| 232 |
|
| 233 |
result, err := r1.Inherits(r2) |
| 234 |
|
| 235 |
if err != nil { |
| 236 |
t.Errorf("Inherits(%s,%s) should not fail. Error: %s", r1.String(), r2.String(), err.Error()) |
| 237 |
} |
| 238 |
|
| 239 |
if result.String() != in2 { |
| 240 |
t.Errorf("Inherits(%s,%s) should be %s but is %s", in1, in2, in2, result) |
| 241 |
} |
| 242 |
|
| 243 |
if result.GetPointer().String() != "" { |
| 244 |
t.Errorf("result(%v)::GetPointer() %v expect %v", result.String(), result.GetPointer().String(), "") |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
func TestInheritsEmptyFragment(t *testing.T) { |
| 249 |
in1 := "#a" |
| 250 |
in2 := "" |
| 251 |
|
| 252 |
r1, _ := NewJsonReference(in1) |
| 253 |
r2, _ := NewJsonReference(in2) |
| 254 |
|
| 255 |
result, err := r1.Inherits(r2) |
| 256 |
|
| 257 |
if err != nil { |
| 258 |
t.Errorf("Inherits(%s,%s) should not fail. Error: %s", r1.String(), r2.String(), err.Error()) |
| 259 |
} |
| 260 |
|
| 261 |
if result.String() != in2 { |
| 262 |
t.Errorf("Inherits(%s,%s) should be empty but is %s", in1, in2, result) |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
func TestFileScheme(t *testing.T) { |
| 267 |
|
| 268 |
in1 := "file:///Users/mac/1.json#a" |
| 269 |
in2 := "file:///Users/mac/2.json#b" |
| 270 |
|
| 271 |
r1, _ := NewJsonReference(in1) |
| 272 |
r2, _ := NewJsonReference(in2) |
| 273 |
|
| 274 |
if r1.HasFragmentOnly != false { |
| 275 |
t.Errorf("NewJsonReference(%v)::HasFragmentOnly %v expect %v", in1, r1.HasFragmentOnly, false) |
| 276 |
} |
| 277 |
|
| 278 |
if r1.HasFileScheme != true { |
| 279 |
t.Errorf("NewJsonReference(%v)::HasFileScheme %v expect %v", in1, r1.HasFileScheme, true) |
| 280 |
} |
| 281 |
|
| 282 |
if r1.HasFullFilePath != true { |
| 283 |
t.Errorf("NewJsonReference(%v)::HasFullFilePath %v expect %v", in1, r1.HasFullFilePath, true) |
| 284 |
} |
| 285 |
|
| 286 |
if r1.IsCanonical() != true { |
| 287 |
t.Errorf("NewJsonReference(%v)::IsCanonical %v expect %v", in1, r1.IsCanonical(), true) |
| 288 |
} |
| 289 |
|
| 290 |
result, err := r1.Inherits(r2) |
| 291 |
if err != nil { |
| 292 |
t.Errorf("Inherits(%s,%s) should not fail. Error: %s", r1.String(), r2.String(), err.Error()) |
| 293 |
} |
| 294 |
if result.String() != in2 { |
| 295 |
t.Errorf("Inherits(%s,%s) should be %s but is %s", in1, in2, in2, result) |
| 296 |
} |
| 297 |
|
| 298 |
if result.GetPointer().String() != "" { |
| 299 |
t.Errorf("result(%v)::GetPointer() %v expect %v", result.String(), result.GetPointer().String(), "") |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
func TestReferenceResolution(t *testing.T) { |
| 304 |
|
| 305 |
// 5.4. Reference Resolution Examples |
| 306 |
// http://tools.ietf.org/html/rfc3986#section-5.4 |
| 307 |
|
| 308 |
base := "http://a/b/c/d;p?q" |
| 309 |
baseRef, err := NewJsonReference(base) |
| 310 |
|
| 311 |
if err != nil { |
| 312 |
t.Errorf("NewJsonReference(%s) failed error: %s", base, err.Error()) |
| 313 |
} |
| 314 |
if baseRef.String() != base { |
| 315 |
t.Errorf("NewJsonReference(%s) %s expected %s", base, baseRef.String(), base) |
| 316 |
} |
| 317 |
|
| 318 |
checks := []string{ |
| 319 |
// 5.4.1. Normal Examples |
| 320 |
// http://tools.ietf.org/html/rfc3986#section-5.4.1 |
| 321 |
|
| 322 |
"g:h", "g:h", |
| 323 |
"g", "http://a/b/c/g", |
| 324 |
"./g", "http://a/b/c/g", |
| 325 |
"g/", "http://a/b/c/g/", |
| 326 |
"/g", "http://a/g", |
| 327 |
"//g", "http://g", |
| 328 |
"?y", "http://a/b/c/d;p?y", |
| 329 |
"g?y", "http://a/b/c/g?y", |
| 330 |
"#s", "http://a/b/c/d;p?q#s", |
| 331 |
"g#s", "http://a/b/c/g#s", |
| 332 |
"g?y#s", "http://a/b/c/g?y#s", |
| 333 |
";x", "http://a/b/c/;x", |
| 334 |
"g;x", "http://a/b/c/g;x", |
| 335 |
"g;x?y#s", "http://a/b/c/g;x?y#s", |
| 336 |
"", "http://a/b/c/d;p?q", |
| 337 |
".", "http://a/b/c/", |
| 338 |
"./", "http://a/b/c/", |
| 339 |
"..", "http://a/b/", |
| 340 |
"../", "http://a/b/", |
| 341 |
"../g", "http://a/b/g", |
| 342 |
"../..", "http://a/", |
| 343 |
"../../", "http://a/", |
| 344 |
"../../g", "http://a/g", |
| 345 |
|
| 346 |
// 5.4.2. Abnormal Examples |
| 347 |
// http://tools.ietf.org/html/rfc3986#section-5.4.2 |
| 348 |
|
| 349 |
"../../../g", "http://a/g", |
| 350 |
"../../../../g", "http://a/g", |
| 351 |
|
| 352 |
"/./g", "http://a/g", |
| 353 |
"/../g", "http://a/g", |
| 354 |
"g.", "http://a/b/c/g.", |
| 355 |
".g", "http://a/b/c/.g", |
| 356 |
"g..", "http://a/b/c/g..", |
| 357 |
"..g", "http://a/b/c/..g", |
| 358 |
|
| 359 |
"./../g", "http://a/b/g", |
| 360 |
"./g/.", "http://a/b/c/g/", |
| 361 |
"g/./h", "http://a/b/c/g/h", |
| 362 |
"g/../h", "http://a/b/c/h", |
| 363 |
"g;x=1/./y", "http://a/b/c/g;x=1/y", |
| 364 |
"g;x=1/../y", "http://a/b/c/y", |
| 365 |
|
| 366 |
"g?y/./x", "http://a/b/c/g?y/./x", |
| 367 |
"g?y/../x", "http://a/b/c/g?y/../x", |
| 368 |
"g#s/./x", "http://a/b/c/g#s/./x", |
| 369 |
"g#s/../x", "http://a/b/c/g#s/../x", |
| 370 |
|
| 371 |
"http:g", "http:g", // for strict parsers |
| 372 |
//"http:g", "http://a/b/c/g", // for backward compatibility |
| 373 |
|
| 374 |
} |
| 375 |
for i := 0; i < len(checks); i += 2 { |
| 376 |
child := checks[i] |
| 377 |
expected := checks[i+1] |
| 378 |
// fmt.Printf("%d: %v -> %v\n", i/2, child, expected) |
| 379 |
|
| 380 |
childRef, e := NewJsonReference(child) |
| 381 |
if e != nil { |
| 382 |
t.Errorf("%d: NewJsonReference(%s) failed error: %s", i/2, child, e.Error()) |
| 383 |
} |
| 384 |
|
| 385 |
res, e := baseRef.Inherits(childRef) |
| 386 |
if res == nil { |
| 387 |
t.Errorf("%d: Inherits(%s, %s) nil not expected", i/2, base, child) |
| 388 |
} |
| 389 |
if e != nil { |
| 390 |
t.Errorf("%d: Inherits(%s) failed error: %s", i/2, child, e.Error()) |
| 391 |
} |
| 392 |
if res.String() != expected { |
| 393 |
t.Errorf("%d: Inherits(%s, %s) %s expected %s", i/2, base, child, res.String(), expected) |
| 394 |
} |
| 395 |
} |
| 396 |
} |