Back to Repositories

Testing Base URL Transformation Logic in DevDocs Documentation System

This test suite validates the functionality of the ApplyBaseUrlFilter class in the DevDocs documentation system. It ensures proper URL rewriting behavior when base URLs are present in HTML documents, focusing on various URL types and edge cases.

Test Coverage Overview

The test suite comprehensively covers URL rewriting scenarios with and without base URLs. Key functionality includes:

  • Base URL absence handling
  • Relative URL transformations
  • Special URL type preservation (absolute, protocol-less, root-relative)
  • Media element URL handling (images, iframes)
  • Edge cases like fragment-only and email URLs

Implementation Analysis

The testing approach utilizes Minitest’s spec-style syntax with context-based organization. It implements the FilterTestHelper module for shared functionality and employs custom helper methods for HTML document construction. The tests follow a consistent pattern of setting up HTML content, applying the filter, and asserting expected transformations.

Technical Details

Testing tools and configuration:

  • Minitest as the testing framework
  • FilterTestHelper for shared test functionality
  • Custom make_body helper method for HTML generation
  • HTML parsing and manipulation capabilities
  • Integration with Docs::ApplyBaseUrlFilter class

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Clear test organization using contexts
  • Isolated test cases for specific functionality
  • Comprehensive edge case coverage
  • DRY principle through helper methods
  • Readable and maintainable test structure
  • Explicit test descriptions

freecodecamp/devdocs

test/lib/docs/filters/core/apply_base_url_test.rb

            
require_relative '../../../../test_helper'
require_relative '../../../../../lib/docs'

class ApplyBaseUrlFilterTest < Minitest::Spec
  include FilterTestHelper
  self.filter_class = Docs::ApplyBaseUrlFilter
  self.filter_type = 'html'

  context "when there is no <base>" do
    it "does nothing" do
      @body = make_body nil, link_to('test')
      assert_equal link_to('test'), filter_output.at_css('body').inner_html
    end
  end

  context "when <base> is '/base/'" do
    it "rewrites relative urls" do
      @body = make_body '/base/', link_to('path#frag')
      assert_equal link_to('/base/path#frag'), filter_output.at_css('body').inner_html
    end

    it "rewrites relative image urls" do
      @body = make_body '/base/', '<img src="../img.png">'
      assert_equal '<img src="/base/../img.png">', filter_output.at_css('body').inner_html
    end

    it "rewrites relative iframe urls" do
      @body = make_body '/base/', '<iframe src="./test"></iframe>'
      assert_equal '<iframe src="/base/./test"></iframe>', filter_output.at_css('body').inner_html
    end

    it "doesn't rewrite absolute urls" do
      @body = make_body '/base/', link_to('http://example.com')
      assert_equal link_to('http://example.com'), filter_output.at_css('body').inner_html
    end

    it "doesn't rewrite protocol-less urls" do
      @body = make_body '/base/', link_to('//example.com')
      assert_equal link_to('//example.com'), filter_output.at_css('body').inner_html
    end

    it "doesn't rewrite root-relative urls" do
      @body = make_body '/base/', link_to('/path')
      assert_equal link_to('/path'), filter_output.at_css('body').inner_html
    end

    it "doesn't rewrite fragment-only urls" do
      @body = make_body '/base/', link_to('#test')
      assert_equal link_to('#test'), filter_output.at_css('body').inner_html
    end

    it "doesn't rewrite email urls" do
      @body = make_body '/base/', link_to('mailto:[email protected]')
      assert_equal link_to('mailto:[email protected]'), filter_output.at_css('body').inner_html
    end

    it "doesn't rewrite data urls" do
      @body = make_body '/base/', '<img src="data:image/gif;base64,aaaa">'
      assert_equal '<img src="data:image/gif;base64,aaaa">', filter_output.at_css('body').inner_html
    end
  end

  private

  def make_body(base, body)
    base = %(<base href="#{base}">) if base
    "<html><meta charset=utf-8><title></title>#{base}#{body}</html>"
  end
end