Skip to content

Conversation

Caplost
Copy link
Contributor

@Caplost Caplost commented Aug 1, 2025

Overview

This PR adds comprehensive unit tests for the Context.Negotiate() method, improving test coverage for Gin framework's content negotiation functionality.

Changes

New Test Cases

Added TestContextNegotiate test function in gin/context_test.go with 14 comprehensive sub-tests:

Basic Format Negotiation Tests

  • JSON Negotiation: Validates application/json Accept header handling
  • HTML Negotiation: Validates text/html Accept header and template rendering
  • XML Negotiation: Validates application/xml Accept header handling
  • YAML Negotiation: Validates application/x-yaml and application/yaml Accept headers
  • TOML Negotiation: Validates application/toml Accept header handling

Advanced Feature Tests

  • Data Fallback Mechanism: Tests fallback to Data field when specific format data is unavailable
  • Data Precedence: Validates that specific format data takes precedence over generic Data field
  • Quality Value Processing: Tests Accept header quality value (q-value) parsing and priority selection
  • Wildcard Support: Validates */* Accept header handling
  • No Accept Header: Tests default behavior (selects first offered format)
  • Unsupported Format: Validates 406 Not Acceptable status code return
  • Partial Matching: Tests partial matching Accept headers like text/*
  • YAML2 MIME Type: Tests application/yaml MIME type support
  • Complex Accept Header: Tests complex Accept headers with multiple types and quality values

Technical Details

Test Coverage Areas

  1. Content Negotiation Algorithm: Validates logic for selecting appropriate response format based on client Accept headers
  2. MIME Type Handling: Tests recognition and processing of various standard MIME types
  3. Quality Value Parsing: Validates correct parsing and priority sorting of HTTP quality values (q-values)
  4. Error Handling: Ensures unsupported formats correctly return error status
  5. Template Rendering: Validates correct HTML template setup and rendering

Code Quality Assurance

  • All test cases pass ✅
  • Follows existing code style and testing patterns
  • Uses httptest for HTTP request/response simulation
  • Employs table-driven testing patterns for improved maintainability

Impact Scope

  • Files Modified: gin/context_test.go
  • New Code: ~200 lines of test code
  • Test Coverage: Improves test coverage for Context.Negotiate() method
  • Backward Compatibility: No breaking changes, only test additions

Verification

# Run specific test
go test -v -run TestContextNegotiate

# Run complete test suite
go test ./...

Related Issues/Tasks

  • Completes Context.Negotiate() method testing task from gin-test-todo-list.md
  • Improves overall project test coverage
  • Provides reliable test foundation for future feature development

Checklist

  • All test cases pass
  • Code style follows project standards
  • No breaking changes
  • Comprehensive functional scenario testing
  • Updated TODO list progress

Caplost and others added 4 commits July 22, 2025 10:10
- Add TestDebugPrintWARNINGDefaultLowGoVersion to test Go version warning branch
- Add TestDebugPrintWithCustomFunc to test custom DebugPrintFunc
- Improve debugPrint function coverage from 75.0% to 100.0%
- Improve getMinVer function coverage to 100.0%
- Add comprehensive test cases for previously untested code paths
- Add TestContextGetRawDataNilBody to cover Request.Body nil case
- Add TestContextSetCookieData SameSiteDefaultMode test case
- Add TestContextInitFormCacheError to cover multipart form parse error
- Add TestContextShouldBindBodyWithReadError to cover body read error
- Add TestContextFormFileParseMultipartFormFailed to cover ParseMultipartForm error
- Add TestSaveUploadedFileChmodFailed to cover chmod error case

These additions improve overall test coverage for context.go functions.
Add test cases covering:
- JSON, HTML, XML, YAML, TOML content negotiation
- Accept header parsing with quality values
- Fallback mechanisms and data precedence
- Wildcard and partial matching
- Error handling for unsupported formats

All tests pass successfully.
@appleboy appleboy requested a review from Copilot August 2, 2025 04:28
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds comprehensive unit tests for the Context.Negotiate() method, significantly improving test coverage for Gin framework's content negotiation functionality. The tests validate various aspects of content negotiation including MIME type handling, quality value processing, fallback mechanisms, and error scenarios.

Key changes include:

  • Addition of 14 comprehensive test cases covering different content negotiation scenarios
  • Tests for JSON, HTML, XML, YAML, and TOML format handling
  • Validation of Accept header parsing with quality values and wildcards
  • Error handling tests for unsupported formats

Reviewed Changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 2 comments.

File Description
context_test.go Adds comprehensive TestContextNegotiate function with 14 sub-tests covering content negotiation scenarios, plus additional edge case tests for form handling, file operations, and cookie management
debug_test.go Adds tests for debug printing functionality including Go version warnings and custom debug print functions


// Should choose XML as it appears first in the Offered slice
assert.Equal(t, http.StatusOK, w.Code)
assert.Contains(t, w.Header().Get("Content-Type"), "application/xml")
Copy link
Preview

Copilot AI Aug 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment incorrectly explains the behavior. According to HTTP content negotiation rules, JSON should be chosen because it has a higher quality value (q=1.0) compared to XML (q=0.9), not because of the order in the Offered slice.

Suggested change
assert.Contains(t, w.Header().Get("Content-Type"), "application/xml")
// Should choose JSON because it has a higher quality value (q=1.0) than XML (q=0.9)
assert.Equal(t, http.StatusOK, w.Code)
assert.Contains(t, w.Header().Get("Content-Type"), "application/json")

Copilot uses AI. Check for mistakes.

JSONData: data,
})

// Should choose XML as it's explicitly mentioned in Accept header
Copy link
Preview

Copilot AI Aug 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is misleading. The test should clarify that XML is chosen because it matches the Accept header and is available in the Offered slice, not simply because it's 'explicitly mentioned' (since HTML is also explicitly mentioned but not offered).

Suggested change
// Should choose XML as it's explicitly mentioned in Accept header
// Should choose XML because it matches the Accept header and is available in the Offered slice,
// not simply because it's explicitly mentioned (HTML is also mentioned but not offered).

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant