Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
pkg/tool/http: fix handling of errors
Browse files Browse the repository at this point in the history
Fixes #451

Change-Id: I75e34ecf87dbc562acaaadc401f5323a0d78e717
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6901
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Aug 20, 2020
1 parent 67d1d42 commit c8a4514
Showing 2 changed files with 96 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/tool/http/http.go
Original file line number Diff line number Diff line change
@@ -99,7 +99,7 @@ func parseHeaders(obj cue.Value, label string) (http.Header, error) {
if err != nil {
return nil, err
}
var h http.Header
h := http.Header{}
for iter.Next() {
str, err := iter.Value().String()
if err != nil {
95 changes: 95 additions & 0 deletions pkg/tool/http/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright 2020 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package http

import (
"fmt"
"strings"
"testing"

"cuelang.org/go/cue"
)

func TestParseHeaders(t *testing.T) {
req := `
header: {
"Accept-Language": "en,nl"
}
trailer: {
"Accept-Language": "en,nl"
User: "foo"
}
`
testCases := []struct {
req string
field string
out string
}{{
field: "header",
out: "nil",
}, {
req: req,
field: "non-exist",
out: "nil",
}, {
req: req,
field: "header",
out: "Accept-Language: en,nl\r\n",
}, {
req: req,
field: "trailer",
out: "Accept-Language: en,nl\r\nUser: foo\r\n",
}, {
req: `
header: {
"1": 'a'
}
`,
field: "header",
out: "header.\"1\": cannot use value 'a' (type bytes) as string",
}, {
req: `
header: 1
`,
field: "header",
out: "header: cannot use value 1 (type int) as struct",
}}
for _, tc := range testCases {
t.Run("", func(t *testing.T) {
r := cue.Runtime{}
inst, err := r.Compile("http headers", tc.req)
if err != nil {
t.Fatal(err)
}

h, err := parseHeaders(inst.Value(), tc.field)

b := &strings.Builder{}
switch {
case err != nil:
fmt.Fprint(b, err)
case h == nil:
b.WriteString("nil")
default:
_ = h.Write(b)
}

got := b.String()
if got != tc.out {
t.Errorf("got %q; want %q", got, tc.out)
}
})
}
}

0 comments on commit c8a4514

Please sign in to comment.