|
1 | 1 | //! Integration tests for FetchKit using wiremock |
2 | 2 |
|
3 | | -use fetchkit::{fetch, FetchRequest, HttpMethod, Tool}; |
| 3 | +use fetchkit::{ |
| 4 | + fetch, fetch_with_options, FetchOptions, FetchRequest, FetcherRegistry, HttpMethod, Tool, |
| 5 | +}; |
4 | 6 | use wiremock::matchers::{method, path}; |
5 | 7 | use wiremock::{Mock, MockServer, ResponseTemplate}; |
6 | 8 |
|
@@ -442,3 +444,158 @@ async fn test_excessive_newlines_filtered() { |
442 | 444 | // Should have at most 2 consecutive newlines |
443 | 445 | assert!(!resp.content.unwrap().contains("\n\n\n")); |
444 | 446 | } |
| 447 | + |
| 448 | +// ============================================================================ |
| 449 | +// Fetcher System Integration Tests |
| 450 | +// ============================================================================ |
| 451 | + |
| 452 | +#[tokio::test] |
| 453 | +async fn test_fetcher_registry_with_defaults() { |
| 454 | + let mock_server = MockServer::start().await; |
| 455 | + |
| 456 | + Mock::given(method("GET")) |
| 457 | + .and(path("/page")) |
| 458 | + .respond_with( |
| 459 | + ResponseTemplate::new(200) |
| 460 | + .set_body_string("<html><body><h1>Test</h1></body></html>") |
| 461 | + .insert_header("content-type", "text/html"), |
| 462 | + ) |
| 463 | + .mount(&mock_server) |
| 464 | + .await; |
| 465 | + |
| 466 | + let registry = FetcherRegistry::with_defaults(); |
| 467 | + let options = FetchOptions { |
| 468 | + enable_markdown: true, |
| 469 | + enable_text: true, |
| 470 | + ..Default::default() |
| 471 | + }; |
| 472 | + |
| 473 | + let req = FetchRequest::new(format!("{}/page", mock_server.uri())).as_markdown(); |
| 474 | + let resp = registry.fetch(req, options).await.unwrap(); |
| 475 | + |
| 476 | + assert_eq!(resp.status_code, 200); |
| 477 | + assert_eq!(resp.format, Some("markdown".to_string())); |
| 478 | + assert!(resp.content.unwrap().contains("# Test")); |
| 479 | +} |
| 480 | + |
| 481 | +#[tokio::test] |
| 482 | +async fn test_fetcher_registry_url_validation() { |
| 483 | + let registry = FetcherRegistry::with_defaults(); |
| 484 | + let options = FetchOptions::default(); |
| 485 | + |
| 486 | + // Invalid scheme |
| 487 | + let req = FetchRequest::new("ftp://example.com"); |
| 488 | + let result = registry.fetch(req, options.clone()).await; |
| 489 | + assert!(result.is_err()); |
| 490 | + assert!(result.unwrap_err().to_string().contains("http://")); |
| 491 | + |
| 492 | + // Empty URL handled by fetch_with_options before registry |
| 493 | + let req = FetchRequest::new(""); |
| 494 | + let result = fetch_with_options(req, options).await; |
| 495 | + assert!(result.is_err()); |
| 496 | +} |
| 497 | + |
| 498 | +#[tokio::test] |
| 499 | +async fn test_fetcher_registry_allow_block_lists() { |
| 500 | + let mock_server = MockServer::start().await; |
| 501 | + |
| 502 | + Mock::given(method("GET")) |
| 503 | + .and(path("/")) |
| 504 | + .respond_with(ResponseTemplate::new(200).set_body_string("OK")) |
| 505 | + .mount(&mock_server) |
| 506 | + .await; |
| 507 | + |
| 508 | + let registry = FetcherRegistry::with_defaults(); |
| 509 | + |
| 510 | + // Block list |
| 511 | + let options = FetchOptions { |
| 512 | + block_prefixes: vec!["http://127.0.0.1".to_string()], |
| 513 | + ..Default::default() |
| 514 | + }; |
| 515 | + let req = FetchRequest::new(format!("{}/", mock_server.uri())); |
| 516 | + let result = registry.fetch(req, options).await; |
| 517 | + assert!(result.is_err()); |
| 518 | + assert!(result.unwrap_err().to_string().contains("Blocked")); |
| 519 | + |
| 520 | + // Allow list (not matching) |
| 521 | + let options = FetchOptions { |
| 522 | + allow_prefixes: vec!["https://allowed.com".to_string()], |
| 523 | + ..Default::default() |
| 524 | + }; |
| 525 | + let req = FetchRequest::new(format!("{}/", mock_server.uri())); |
| 526 | + let result = registry.fetch(req, options).await; |
| 527 | + assert!(result.is_err()); |
| 528 | +} |
| 529 | + |
| 530 | +#[tokio::test] |
| 531 | +async fn test_github_fetcher_url_matching() { |
| 532 | + // These URLs should NOT match GitHubRepoFetcher (will use DefaultFetcher) |
| 533 | + let mock_server = MockServer::start().await; |
| 534 | + |
| 535 | + // Mock for non-GitHub URLs |
| 536 | + Mock::given(method("GET")) |
| 537 | + .and(path("/owner/repo/issues")) |
| 538 | + .respond_with( |
| 539 | + ResponseTemplate::new(200) |
| 540 | + .set_body_string("issues page") |
| 541 | + .insert_header("content-type", "text/plain"), |
| 542 | + ) |
| 543 | + .mount(&mock_server) |
| 544 | + .await; |
| 545 | + |
| 546 | + let req = FetchRequest::new(format!("{}/owner/repo/issues", mock_server.uri())); |
| 547 | + let resp = fetch(req).await.unwrap(); |
| 548 | + |
| 549 | + // Should use default fetcher (format is "raw", not "github_repo") |
| 550 | + assert_eq!(resp.format, Some("raw".to_string())); |
| 551 | + assert!(resp.content.unwrap().contains("issues page")); |
| 552 | +} |
| 553 | + |
| 554 | +#[tokio::test] |
| 555 | +async fn test_fetch_enables_conversions_by_default() { |
| 556 | + let mock_server = MockServer::start().await; |
| 557 | + |
| 558 | + Mock::given(method("GET")) |
| 559 | + .and(path("/")) |
| 560 | + .respond_with( |
| 561 | + ResponseTemplate::new(200) |
| 562 | + .set_body_string("<html><body><p>Hello</p></body></html>") |
| 563 | + .insert_header("content-type", "text/html"), |
| 564 | + ) |
| 565 | + .mount(&mock_server) |
| 566 | + .await; |
| 567 | + |
| 568 | + // Using fetch() with as_markdown() should work |
| 569 | + let req = FetchRequest::new(format!("{}/", mock_server.uri())).as_markdown(); |
| 570 | + let resp = fetch(req).await.unwrap(); |
| 571 | + |
| 572 | + assert_eq!(resp.format, Some("markdown".to_string())); |
| 573 | +} |
| 574 | + |
| 575 | +#[tokio::test] |
| 576 | +async fn test_fetch_with_options_respects_disabled_conversion() { |
| 577 | + let mock_server = MockServer::start().await; |
| 578 | + |
| 579 | + Mock::given(method("GET")) |
| 580 | + .and(path("/")) |
| 581 | + .respond_with( |
| 582 | + ResponseTemplate::new(200) |
| 583 | + .set_body_string("<html><body><p>Hello</p></body></html>") |
| 584 | + .insert_header("content-type", "text/html"), |
| 585 | + ) |
| 586 | + .mount(&mock_server) |
| 587 | + .await; |
| 588 | + |
| 589 | + // Disable markdown conversion |
| 590 | + let options = FetchOptions { |
| 591 | + enable_markdown: false, |
| 592 | + enable_text: false, |
| 593 | + ..Default::default() |
| 594 | + }; |
| 595 | + |
| 596 | + let req = FetchRequest::new(format!("{}/", mock_server.uri())).as_markdown(); |
| 597 | + let resp = fetch_with_options(req, options).await.unwrap(); |
| 598 | + |
| 599 | + // Should be raw because conversion is disabled |
| 600 | + assert_eq!(resp.format, Some("raw".to_string())); |
| 601 | +} |
0 commit comments