Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 [Bug]: Using Head Method Header ContentLength always set and is not zero #2703

Closed
3 tasks done
afifurrohman-id opened this issue Nov 3, 2023 · 8 comments
Closed
3 tasks done

Comments

@afifurrohman-id
Copy link

Bug Description

I don't know why fiber always Set Header ContentLength with non zero value, even using HEAD method

I have try using ctx.Send(nil) still same, even i set header manually ctx.Set(fiber.HeaderContentLength, "") still didn't work

How to Reproduce

Steps to reproduce the behavior:

  1. Run test for code sample.
go test -v 
  1. See..
=== RUN   TestHead
2023/11/04 05:35:45.740429 main_test.go:27: [Fatal] Expected Header ContentLength to zero, but got 18
exit status 1
FAIL    go-fiber-bug-head       0.641s

Expected Behavior

PASS, Header ContentLength is not set OR zero value

Fiber Version

v2.50.0

Code Snippet (optional)

package main

import (
        "testing"
        "net/http/httptest"
        "github.com/gofiber/fiber/v2"
        "github.com/gofiber/fiber/v2/log"
)

func TestHead(test *testing.T) {
        app := fiber.New()
        app.Head("/", func(ctx *fiber.Ctx) error {
                return ctx.SendStatus(fiber.StatusOK)
        })

        req := httptest.NewRequest(fiber.MethodGet, "/", nil)
        res, err := app.Test(req)
        if err != nil {
            log.Error(err)
        }


        contentLength :=  res.Header.Get(fiber.HeaderContentLength)
        if contentLength != "" {
                log.Fatalf("Expected Header ContentLength to zero, but got %s",contentLength)
        }

}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my problem prior to opening this one.
  • I understand that improperly formatted bug reports may be closed without explanation.
Copy link

welcome bot commented Nov 3, 2023

Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@ReneWerner87
Copy link
Member

It is an automatism, within our kernel, which sets this header automatically and is not a bug

This is intentional

@afifurrohman-id
Copy link
Author

Ok so, how when i want to unset ContentLength header??

@ReneWerner87
Copy link
Member

Why would you want to do that?

@ReneWerner87
Copy link
Member

This is determined automatically based on the content.
I can send you an example later of how you can set it to 0, but not unset.

@ReneWerner87
Copy link
Member

ReneWerner87 commented Nov 5, 2023

@afifurrohman-id

image

app.Head("/", func(ctx *fiber.Ctx) error {
    return ctx.Status(fiber.StatusOK).Send(nil)
})

image

app.Head("/", func(ctx *fiber.Ctx) error {
    // manipulation of the content length
    ctx.Response().Header.SetContentLength(10000)
    return ctx.Status(fiber.StatusOK).Send(nil)
})

@afifurrohman-id
Copy link
Author

Okay cool, can you added to docs? May helpful for anyone with same question

@ReneWerner87
Copy link
Member

if it comes up again, I'll add it

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

No branches or pull requests

2 participants