Skip to content

Commit

Permalink
parsing error code
Browse files Browse the repository at this point in the history
  • Loading branch information
tednaleid committed Jan 14, 2024
1 parent d4007c6 commit 587be66
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
6 changes: 5 additions & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,11 @@ func ProcessRequests(context *execcontext.Context) {
requestWaitGroup := requests.StartRequestWorkers(requestsWithContextChannel, responsesChannel, context)
responseWaitGroup := responses.StartResponseWorkers(responsesChannel, context)

parser.SendRequests(requestsWithContextChannel, context.In, context.RequestMethod, context.RequestHeaders)
err := parser.SendRequests(requestsWithContextChannel, context.In, context.RequestMethod, context.RequestHeaders)

if err != nil {
context.Logger.LogError(err, "error parsing requests")
}

close(requestsWithContextChannel)
requestWaitGroup.Wait()
Expand Down
19 changes: 10 additions & 9 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ func SendRequests(
in io.Reader,
requestMethod string,
staticHeaders []config.RequestHeader,
) {
) error {
reader := bufio.NewReader(in)
inputType, _ := determineInputType(reader)

if inputType == Urls {
SendUrlsRequests(requestsWithContext, reader, requestMethod, staticHeaders)
} else if inputType == JsonLines {
SendJsonLinesRequests(requestsWithContext, reader, requestMethod, staticHeaders)
if inputType == JsonLines {
return SendJsonLinesRequests(requestsWithContext, reader, requestMethod, staticHeaders)
}

return SendUrlsRequests(requestsWithContext, reader, requestMethod, staticHeaders)
}

// Each line is an URL and optionally some CSV context that can be passed through
Expand All @@ -44,7 +44,7 @@ func SendUrlsRequests(
reader *bufio.Reader,
requestMethod string,
staticHeaders []config.RequestHeader,
) {
) error {
csvReader := csv.NewReader(reader)
csvReader.Comma = '\t'
csvReader.FieldsPerRecord = -1
Expand All @@ -54,8 +54,7 @@ func SendUrlsRequests(
if err == io.EOF {
break
} else if err != nil {
// TODO handle more gracefully when CSV isn't well formed
panic(err)
return err
}

if len(record) > 0 {
Expand All @@ -64,16 +63,18 @@ func SendUrlsRequests(
requestsWithContext <- RequestWithContext{Request: request, RequestContext: record[1:]}
}
}
return nil
}

func SendJsonLinesRequests(
requestsWithContext chan<- RequestWithContext,
reader *bufio.Reader,
requestMethod string,
staticHeaders []config.RequestHeader,
) {
) error {
// TODO

return nil
}

// current assumption is that the first character is '{' for a stream of json lines,
Expand Down
20 changes: 18 additions & 2 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestSendGetRequestUrlsHaveDefaultHeaders(t *testing.T) {
func TestSendRequestsHasRaggedRequestContext(t *testing.T) {
t.Parallel()

requestsWithContext := make(chan parser.RequestWithContext)
requestsWithContext := make(chan parser.RequestWithContext, 3)
defer close(requestsWithContext)

// we allow ragged numbers of fields in the TSV input
Expand All @@ -57,7 +57,7 @@ func TestSendRequestsHasRaggedRequestContext(t *testing.T) {

var in = trimmedInputReader(inputLines)

go parser.SendRequests(requestsWithContext, in, "GET", []config.RequestHeader{})
parser.SendRequests(requestsWithContext, in, "GET", []config.RequestHeader{})

expectedResults := []struct {
url string
Expand All @@ -79,6 +79,22 @@ func TestSendRequestsHasRaggedRequestContext(t *testing.T) {

}

func TestSendRequestsHasMalformedInput(t *testing.T) {
t.Parallel()

requestsWithContext := make(chan parser.RequestWithContext, 1)
defer close(requestsWithContext)

inputLines := `https://ex.com/bar foo "quoted content missing terminating quote`

var in = trimmedInputReader(inputLines)

err := parser.SendRequests(requestsWithContext, in, "GET", []config.RequestHeader{})

assert.NotNil(t, err, "expected error")
assert.Equal(t, err.Error(), "parse error on line 1, column 65: extraneous or missing \" in quoted-field")
}

func inputReader(urls []string) io.Reader {
stringUrls := strings.Join(urls, "\n")
return strings.NewReader(stringUrls)
Expand Down

0 comments on commit 587be66

Please sign in to comment.