comparison OrthancFramework/UnitTestsSources/RestApiTests.cpp @ 5338:78c59b02b121

accept parameters are now provided to HttpContentNegociation::IHandler::Handle()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 28 Jun 2023 08:29:43 +0200
parents 143b45791233
children cb11e5ced4e3
comparison
equal deleted inserted replaced
5337:b376abae664a 5338:78c59b02b121
382 class AcceptHandler : public HttpContentNegociation::IHandler 382 class AcceptHandler : public HttpContentNegociation::IHandler
383 { 383 {
384 private: 384 private:
385 std::string type_; 385 std::string type_;
386 std::string subtype_; 386 std::string subtype_;
387 HttpContentNegociation::Dictionary parameters_;
387 388
388 public: 389 public:
389 AcceptHandler() 390 AcceptHandler()
390 { 391 {
391 Reset(); 392 Reset();
392 } 393 }
393 394
394 void Reset() 395 void Reset()
395 { 396 {
396 Handle("nope", "nope"); 397 HttpContentNegociation::Dictionary parameters;
398 Handle("nope", "nope", parameters);
397 } 399 }
398 400
399 const std::string& GetType() const 401 const std::string& GetType() const
400 { 402 {
401 return type_; 403 return type_;
404 const std::string& GetSubType() const 406 const std::string& GetSubType() const
405 { 407 {
406 return subtype_; 408 return subtype_;
407 } 409 }
408 410
411 HttpContentNegociation::Dictionary& GetParameters()
412 {
413 return parameters_;
414 }
415
409 virtual void Handle(const std::string& type, 416 virtual void Handle(const std::string& type,
410 const std::string& subtype) ORTHANC_OVERRIDE 417 const std::string& subtype,
418 const HttpContentNegociation::Dictionary& parameters) ORTHANC_OVERRIDE
411 { 419 {
412 type_ = type; 420 type_ = type;
413 subtype_ = subtype; 421 subtype_ = subtype;
422 parameters_ = parameters;
414 } 423 }
415 }; 424 };
416 } 425 }
417 426
418 427
428 d.Register("audio/basic", h); 437 d.Register("audio/basic", h);
429 438
430 ASSERT_TRUE(d.Apply("audio/*; q=0.2, audio/basic")); 439 ASSERT_TRUE(d.Apply("audio/*; q=0.2, audio/basic"));
431 ASSERT_EQ("audio", h.GetType()); 440 ASSERT_EQ("audio", h.GetType());
432 ASSERT_EQ("basic", h.GetSubType()); 441 ASSERT_EQ("basic", h.GetSubType());
433 442 ASSERT_EQ(0u, h.GetParameters().size());
434 ASSERT_TRUE(d.Apply("audio/*; q=0.2, audio/nope")); 443
444 ASSERT_TRUE(d.Apply("audio/*; q=0.2 ; type = test ; hello , audio/nope"));
435 ASSERT_EQ("audio", h.GetType()); 445 ASSERT_EQ("audio", h.GetType());
436 ASSERT_EQ("mp3", h.GetSubType()); 446 ASSERT_EQ("mp3", h.GetSubType());
447 ASSERT_EQ(3u, h.GetParameters().size());
448 ASSERT_EQ("0.2", h.GetParameters() ["q"]);
449 ASSERT_EQ("test", h.GetParameters() ["type"]);
450 ASSERT_EQ("", h.GetParameters() ["hello"]);
437 451
438 ASSERT_FALSE(d.Apply("application/*; q=0.2, application/pdf")); 452 ASSERT_FALSE(d.Apply("application/*; q=0.2, application/pdf"));
439 453
440 ASSERT_TRUE(d.Apply("*/*; application/*; q=0.2, application/pdf")); 454 ASSERT_TRUE(d.Apply("*/*; hello=world, application/*; q=0.2, application/pdf"));
441 ASSERT_EQ("audio", h.GetType()); 455 ASSERT_EQ("audio", h.GetType());
456 ASSERT_EQ(1u, h.GetParameters().size());
457 ASSERT_EQ("world", h.GetParameters() ["hello"]);
442 } 458 }
443 459
444 // "This would be interpreted as "text/html and text/x-c are the 460 // "This would be interpreted as "text/html and text/x-c are the
445 // preferred media types, but if they do not exist, then send the 461 // preferred media types, but if they do not exist, then send the
446 // text/x-dvi entity, and if that does not exist, send the 462 // text/x-dvi entity, and if that does not exist, send the
447 // text/plain entity."" 463 // text/plain entity.""
448 const std::string T1 = "text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c"; 464 const std::string T1 = "text/plain; q=0.5, text/html ; hello = world , text/x-dvi; q=0.8, text/x-c";
449 465
450 { 466 {
451 HttpContentNegociation d; 467 HttpContentNegociation d;
452 d.Register("text/plain", h); 468 d.Register("text/plain", h);
453 d.Register("text/html", h); 469 d.Register("text/html", h);
454 d.Register("text/x-dvi", h); 470 d.Register("text/x-dvi", h);
455 ASSERT_TRUE(d.Apply(T1)); 471 ASSERT_TRUE(d.Apply(T1));
456 ASSERT_EQ("text", h.GetType()); 472 ASSERT_EQ("text", h.GetType());
457 ASSERT_EQ("html", h.GetSubType()); 473 ASSERT_EQ("html", h.GetSubType());
474 ASSERT_EQ(1u, h.GetParameters().size());
475 ASSERT_EQ("world", h.GetParameters() ["hello"]);
458 } 476 }
459 477
460 { 478 {
461 HttpContentNegociation d; 479 HttpContentNegociation d;
462 d.Register("text/plain", h); 480 d.Register("text/plain", h);
463 d.Register("text/x-dvi", h); 481 d.Register("text/x-dvi", h);
464 d.Register("text/x-c", h); 482 d.Register("text/x-c", h);
465 ASSERT_TRUE(d.Apply(T1)); 483 ASSERT_TRUE(d.Apply(T1));
466 ASSERT_EQ("text", h.GetType()); 484 ASSERT_EQ("text", h.GetType());
467 ASSERT_EQ("x-c", h.GetSubType()); 485 ASSERT_EQ("x-c", h.GetSubType());
486 ASSERT_EQ(0u, h.GetParameters().size());
468 } 487 }
469 488
470 { 489 {
471 HttpContentNegociation d; 490 HttpContentNegociation d;
472 d.Register("text/plain", h); 491 d.Register("text/plain", h);
474 d.Register("text/x-c", h); 493 d.Register("text/x-c", h);
475 d.Register("text/html", h); 494 d.Register("text/html", h);
476 ASSERT_TRUE(d.Apply(T1)); 495 ASSERT_TRUE(d.Apply(T1));
477 ASSERT_EQ("text", h.GetType()); 496 ASSERT_EQ("text", h.GetType());
478 ASSERT_TRUE(h.GetSubType() == "x-c" || h.GetSubType() == "html"); 497 ASSERT_TRUE(h.GetSubType() == "x-c" || h.GetSubType() == "html");
498 if (h.GetSubType() == "x-c")
499 {
500 ASSERT_EQ(0u, h.GetParameters().size());
501 }
502 else
503 {
504 ASSERT_EQ(1u, h.GetParameters().size());
505 ASSERT_EQ("world", h.GetParameters() ["hello"]);
506 }
479 } 507 }
480 508
481 { 509 {
482 HttpContentNegociation d; 510 HttpContentNegociation d;
483 d.Register("text/plain", h); 511 d.Register("text/plain", h);
484 d.Register("text/x-dvi", h); 512 d.Register("text/x-dvi", h);
485 ASSERT_TRUE(d.Apply(T1)); 513 ASSERT_TRUE(d.Apply(T1));
486 ASSERT_EQ("text", h.GetType()); 514 ASSERT_EQ("text", h.GetType());
487 ASSERT_EQ("x-dvi", h.GetSubType()); 515 ASSERT_EQ("x-dvi", h.GetSubType());
516 ASSERT_EQ(1u, h.GetParameters().size());
517 ASSERT_EQ("0.8", h.GetParameters() ["q"]);
488 } 518 }
489 519
490 { 520 {
491 HttpContentNegociation d; 521 HttpContentNegociation d;
492 d.Register("text/plain", h); 522 d.Register("text/plain", h);
493 ASSERT_TRUE(d.Apply(T1)); 523 ASSERT_TRUE(d.Apply(T1));
494 ASSERT_EQ("text", h.GetType()); 524 ASSERT_EQ("text", h.GetType());
495 ASSERT_EQ("plain", h.GetSubType()); 525 ASSERT_EQ("plain", h.GetSubType());
526 ASSERT_EQ(1u, h.GetParameters().size());
527 ASSERT_EQ("0.5", h.GetParameters() ["q"]);
496 } 528 }
497 } 529 }
498 530
499 531
500 TEST(WebServiceParameters, Serialization) 532 TEST(WebServiceParameters, Serialization)