Skip to content

Commit

Permalink
Skip template parse error in Query2Body
Browse files Browse the repository at this point in the history
Check content-type for Header2Body
  • Loading branch information
Kyaw Myint Thein committed Jan 18, 2022
1 parent 91363c3 commit 2be9db3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
9 changes: 9 additions & 0 deletions header2body.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rzluramartian
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"

Expand All @@ -21,6 +22,10 @@ type (
}
)

var (
_unexpectedContentTypeErr = fmt.Errorf("unexpected content-type")
)

func headerModifierFromJSON(b []byte) (*parse.Result, error) {
cfg := &HeaderModifierConfig{}
if err := json.Unmarshal(b, cfg); err != nil {
Expand All @@ -37,6 +42,10 @@ func headerModifierFromJSON(b []byte) (*parse.Result, error) {
func (m *Header2BodyModifier) ModifyRequest(req *http.Request) error {
var buf []byte

if req.Header.Get(_contentTypeHeader) != _contentType_applicationJSON {
return _unexpectedContentTypeErr
}

if req.Body != nil {
body, err := ioutil.ReadAll(req.Body)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion querystring2body.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type (
Template string `json:"template"`
ContentType string `json:"content_type"`
}

Query2BodyModifier struct {
keysToExtract []string
template *template.Template
Expand Down Expand Up @@ -53,7 +54,7 @@ func (m *Query2BodyModifier) ModifyRequest(req *http.Request) error {

buf := new(bytes.Buffer)
if err := m.template.Execute(buf, query); err != nil {
return err
return nil
}

for _, k := range m.keysToExtract {
Expand Down

0 comments on commit 2be9db3

Please sign in to comment.