Kong / go-pdk

Write Kong plugins in Go! 🦍
https://pkg.go.dev/github.com/Kong/go-pdk
Apache License 2.0
144 stars 47 forks source link

`kong.Request.GetHeaders` returns headers uppercased in test module #138

Closed cakemanny closed 3 months ago

cakemanny commented 1 year ago

kong.Request.GetHeaders should return a map with the headers normalised in lower case. ( https://github.com/Kong/go-pdk/blob/4deec45a5bfe421a0ceebcbf7931d395250949b0/request/request.go#L179-L180 )

~/src/tmp/go-pdk-bug-2 % go test
2023/02/08 17:54:55 header: Cookie
2023/02/08 17:54:55 value: [fun=1]
PASS
ok      plugintest  0.154s

Here above you see the key comes out as Cookie when running the tests. But when running the plugin in kong, it come out as cookie as per the docs. This is the key of the map returned from calling kong.Request.GetHeaders(-1) The full contents of the main and test files are below.

// main.go
package main

import (
    "log"

    "github.com/Kong/go-pdk"
    "github.com/Kong/go-pdk/server"
)

func main() {
    server.StartServer(New, Version, Priority)
}

var Version = "0.0.1"
var Priority = 1

type Config struct{}

func New() interface{} {
    return &Config{}
}

func (conf Config) Access(kong *pdk.PDK) {
    headers, err := kong.Request.GetHeaders(-1)
    if err != nil {
        kong.Response.ExitStatus(500)
    }
    for k, v := range headers {
        log.Printf("header: %v", k)
        log.Printf("value: %v", v)
    }
}
// main_test.go
package main

import (
    "testing"

    "github.com/Kong/go-pdk/test"
    "github.com/stretchr/testify/assert"
)

func TestPlugin(t *testing.T) {
    env, err := test.New(t, test.Request{
        Method: "GET",
        Url:    "http://example.com?bar=baz",
        Headers: map[string][]string{
            "cookie": {"fun=1"},
        },
    })
    if assert.NoError(t, err) {
        env.DoHttps(&Config{})
        assert.Equal(t, 200, env.ClientRes.Status)
    }
}

go.mod

module plugintest

go 1.18

require (
    github.com/Kong/go-pdk v0.8.0
    github.com/stretchr/testify v1.8.0
)

require (
    github.com/davecgh/go-spew v1.1.1 // indirect
    github.com/pmezard/go-difflib v1.0.0 // indirect
    github.com/ugorji/go/codec v1.2.1 // indirect
    google.golang.org/protobuf v1.25.0 // indirect
    gopkg.in/yaml.v3 v3.0.1 // indirect
)
StarlightIbuki commented 1 year ago

You're right: PDK docs.

We will investigate this.

Internally tracked: KAG-3812

gszr commented 3 months ago

Hey @cakemanny, thank you for the report! I've opened https://github.com/Kong/go-pdk/pull/194 fixing the issue.