Skip to content

🐛 [Bug]: Cannot set header to 'text/event-stream' with ctx.Set #2546

@michealroberts

Description

@michealroberts

Bug Description

As above, I had a middleware running on version 2.31.0 of Go finer, where I would utilise the following:

import "github.com/gofiber/fiber/v2"

func SetServerSentEventsHeaders(c *fiber.Ctx) error {
	c.Set("Content-Type", "text/event-stream")
	c.Set("Cache-Control", "no-cache")
	c.Set("Connection", "keep-alive")
	c.Set("Transfer-Encoding", "chunked")
	return c.Next()
}

My associated test suite was passing on version 2.31.0 but on version 2.48.0 it seems to be failing, instead, the Content-Type header is still set to 'text/plain'

Screenshot 2023-07-17 at 15 13 06

How to Reproduce

As above.

Expected Behavior

It should be able to set the Content-Type header to "text/event-stream".

Fiber Version

2.48.0

Code Snippet (optional)

package sse

import "github.com/gofiber/fiber/v2"

func SetServerSentEventsHeaders(c *fiber.Ctx) error {
  c.Set("Content-Type", "text/event-stream")
  c.Set("Cache-Control", "no-cache")
  c.Set("Connection", "keep-alive")
  c.Set("Transfer-Encoding", "chunked")
  return c.Next()
}


func New() fiber.Handler {
  return func(c *fiber.Ctx) error {
    return SetServerSentEventsHeaders(c)
  }
}

Associated test (failing):

package sse

import (
	"testing"

	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/utils"
	"github.com/valyala/fasthttp"
)

func TestSSEDefaults(t *testing.T) {
  app := fiber.New(fiber.Config{
    IdleTimeout: -1,
    ReadTimeout: 0,
  })

  app.Use(New())

  t.Helper()

  h := app.Handler()

  // Test default GET response headers
  ctx := &fasthttp.RequestCtx{}
  ctx.Request.Header.SetMethod(fiber.MethodGet)
  h(ctx)

  utils.AssertEqual(t, "text/event-stream", string(ctx.Response.Header.Peek(fiber.HeaderContentType)))
  utils.AssertEqual(t, "no-cache", string(ctx.Response.Header.Peek(fiber.HeaderCacheControl)))
  utils.AssertEqual(t, "keep-alive", string(ctx.Response.Header.Peek(fiber.HeaderConnection)))

  ctx.Response.Header.Set(fiber.HeaderConnection, "close")
  ctx.Response.ConnectionClose()

  app.Shutdown()
}

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions