|
| 1 | +// Copyright 2015 go-swagger maintainers |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package runtime |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "github.com/stretchr/testify/assert" |
| 20 | + "io" |
| 21 | + "net/http/httptest" |
| 22 | + "testing" |
| 23 | +) |
| 24 | + |
| 25 | +const consProdCSV = `name,country,age |
| 26 | +John,US,19 |
| 27 | +Mike,US,20 |
| 28 | +` |
| 29 | + |
| 30 | +type csvEmptyReader struct{} |
| 31 | + |
| 32 | +func (r *csvEmptyReader) Read(d []byte) (int, error) { |
| 33 | + return 0, io.EOF |
| 34 | +} |
| 35 | + |
| 36 | +func TestCSVConsumer(t *testing.T) { |
| 37 | + cons := CSVConsumer() |
| 38 | + reader := bytes.NewBuffer([]byte(consProdCSV)) |
| 39 | + |
| 40 | + outBuf := new(bytes.Buffer) |
| 41 | + err := cons.Consume(reader, outBuf) |
| 42 | + assert.NoError(t, err) |
| 43 | + assert.Equal(t, consProdCSV, outBuf.String()) |
| 44 | + |
| 45 | + outBuf2 := new(bytes.Buffer) |
| 46 | + err = cons.Consume(nil, outBuf2) |
| 47 | + assert.Error(t, err) |
| 48 | + |
| 49 | + err = cons.Consume(reader, struct{}{}) |
| 50 | + assert.Error(t, err) |
| 51 | + |
| 52 | + emptyOutBuf := new(bytes.Buffer) |
| 53 | + err = cons.Consume(&csvEmptyReader{}, emptyOutBuf) |
| 54 | + assert.NoError(t, err) |
| 55 | + assert.Equal(t, "", emptyOutBuf.String()) |
| 56 | +} |
| 57 | + |
| 58 | +func TestCSVProducer(t *testing.T) { |
| 59 | + prod := CSVProducer() |
| 60 | + data := []byte(consProdCSV) |
| 61 | + |
| 62 | + rw := httptest.NewRecorder() |
| 63 | + err := prod.Produce(rw, data) |
| 64 | + assert.NoError(t, err) |
| 65 | + assert.Equal(t, consProdCSV, rw.Body.String()) |
| 66 | + |
| 67 | + rw2 := httptest.NewRecorder() |
| 68 | + err = prod.Produce(rw2, struct{}{}) |
| 69 | + assert.Error(t, err) |
| 70 | + |
| 71 | + err = prod.Produce(nil, data) |
| 72 | + assert.Error(t, err) |
| 73 | +} |
0 commit comments