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

🐞 app.Static serving index.html as text/plain #420

Closed
soulchainer opened this issue May 29, 2020 · 8 comments
Closed

🐞 app.Static serving index.html as text/plain #420

soulchainer opened this issue May 29, 2020 · 8 comments

Comments

@soulchainer
Copy link
Contributor

Using Fiber v1.10.1

Question description

I'm not sure if this is a bug or I understood it wrong.
It seems the root route must be defined first of all routes, otherwise its content will be rendered as plain text.

Code snippet (optional)

This will result in index.html, at /, being rendered as text/plain:

package main

import "github.com/gofiber/fiber"

func main() {
  app := fiber.New()

  app.Static("/otherPath", otherPath) // I'm using here an absolute path
  app.Static("/", "./public")

  app.Listen(8000)
}

This will result in index.html, at /, being rendered as text/html:

package main

import "github.com/gofiber/fiber"

func main() {
  app := fiber.New()

  app.Static("/", "./public")
  app.Static("/otherPath", otherPath)

  app.Listen(8000)
}

Of course, this is not a big deal. It's customary to put the root first most of the time, but even so I just don't know, is this intended behavior or...?

Thanks for your time!

@welcome
Copy link

welcome bot commented May 29, 2020

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

@Fenny
Copy link
Member

Fenny commented May 29, 2020

@soulchainer I couldn't produce the same result, by any chance you are using Windows? Because it could be caused by a specific windows update that changed the MIME types in the registry.

@thomasvvugt do you remember how you solved this issue?

To make sure it's OS related I added an additional test case #421

@Fenny Fenny changed the title app.Static serving index.html as text/plain 🐞 🐞 app.Static serving index.html as text/plain May 29, 2020
@soulchainer
Copy link
Contributor Author

@Fenny No, I'm using Arch Linux.
And is neither a browser issue (checked in Firefox/Firefox developer edition/Chromium, the browsers I usually use).

Fiber v1.10.1
go version go1.14.3 linux/amd64
Linux 5.6.4-arch1-1

@Fenny
Copy link
Member

Fenny commented May 29, 2020

Could you try to run this test app_test.go#L208 on your machine (there is an index.html test file in ./github)

Could you also share the <html>, <head> content of your index.html file?

@soulchainer
Copy link
Contributor Author

@Fenny That test runs OK, because it doesn't cover this case, but only the case where it already works.

As I said, if you have app.Static("/", "./.github") appearing first in your code, it works fine (the case you're checking in your test).

If you edit that test adding the extra line that I add below, which covers this error case:

func Test_App_Static_Index(t *testing.T) {
	app := New()

        // I added this line, some folder in your machine, obviosly this should be some other general file, quick check for my case
	app.Static("/music", "/home/soulchainer/Music")
	app.Static("/", "./.github")

	req := httptest.NewRequest("GET", "/", nil)
	resp, err := app.Test(req)
	utils.AssertEqual(t, nil, err, "app.Test(req)")
	utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
	utils.AssertEqual(t, false, resp.Header.Get("Content-Length") == "")
	utils.AssertEqual(t, "text/html; charset=utf-8", resp.Header.Get("Content-Type"))

	body, err := ioutil.ReadAll(resp.Body)
	utils.AssertEqual(t, nil, err)
	utils.AssertEqual(t, true, strings.Contains(string(body), "Hello, World!"))
}

It fails like this:

--- FAIL: Test_App_Static_Index (0.00s)
    utils.go:169: 
        Test:       Test_App_Static_Index
        Trace:      app_test.go:219
        Error:      Not equal
        Expect:     text/html; charset=utf-8      [string]
        Result:     text/plain; charset=utf-8     [string]
FAIL
FAIL	github.com/gofiber/fiber	0.008s
FAIL

Which is what I said is happening.

My current index.html file is almost the same that the one you wrote here, because I was just trying Fiber and it was an Hello world. Is this:

index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Cliente</title>
</head>
<body>
  Hello world!
</body>
</html>

@Fenny
Copy link
Member

Fenny commented May 29, 2020

Thank you for your detailed bug report, I will debug this today and keep you posted 👍

@ReneWerner87
Copy link
Member

am also interested in this bug as I noticed the same behavior in one of our microservices, had debugged it before and was able to trace it back to fasthttp, where the default content type is used

Take a closer look tomorrow

Fenny added a commit to Fenny/fiber that referenced this issue Jun 1, 2020
@Fenny Fenny mentioned this issue Jun 1, 2020
@Fenny
Copy link
Member

Fenny commented Jun 1, 2020

The content-type issue has been fixed in v1.10.2

Thank you for your issue 👍

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

3 participants