| 28 | var testDatasourceInvalidMethod string |
| 29 | |
| 30 | func TestHttpDataSource(t *testing.T) { |
| 31 | tests := []struct { |
| 32 | Name string |
| 33 | Path string |
| 34 | Error bool |
| 35 | ExpectedOutputs map[string]string |
| 36 | }{ |
| 37 | { |
| 38 | Name: "basic_test", |
| 39 | Path: testDatasourceBasic, |
| 40 | Error: false, |
| 41 | ExpectedOutputs: map[string]string{ |
| 42 | "url": "url is https://www.google.com", |
| 43 | // Check that body is not empty |
| 44 | "body": "body is true", |
| 45 | }, |
| 46 | }, |
| 47 | { |
| 48 | Name: "url_is_empty", |
| 49 | Path: testDatasourceEmptyUrl, |
| 50 | Error: true, |
| 51 | ExpectedOutputs: map[string]string{ |
| 52 | "error": "the `url` must be specified", |
| 53 | }, |
| 54 | }, |
| 55 | { |
| 56 | Name: "method_is_invalid", |
| 57 | Path: testDatasourceInvalidMethod, |
| 58 | Error: true, |
| 59 | ExpectedOutputs: map[string]string{ |
| 60 | "error": "the `method` must be one of \\[HEAD GET POST PUT DELETE OPTIONS PATCH\\]", |
| 61 | }, |
| 62 | }, |
| 63 | { |
| 64 | Name: "404_url", |
| 65 | Path: testDatasource404Url, |
| 66 | Error: true, |
| 67 | }, |
| 68 | } |
| 69 | for _, tt := range tests { |
| 70 | t.Run(tt.Name, func(t *testing.T) { |
| 71 | testCase := &acctest.PluginTestCase{ |
| 72 | Name: tt.Name, |
| 73 | Setup: func() error { |
| 74 | return nil |
| 75 | }, |
| 76 | Teardown: func() error { |
| 77 | return nil |
| 78 | }, |
| 79 | Template: tt.Path, |
| 80 | Type: "http", |
| 81 | Check: func(buildCommand *exec.Cmd, logfile string) error { |
| 82 | if buildCommand.ProcessState != nil { |
| 83 | if buildCommand.ProcessState.ExitCode() != 0 && !tt.Error { |
| 84 | return fmt.Errorf("Bad exit code. Logfile: %s", logfile) |
| 85 | } |
| 86 | if tt.Error && buildCommand.ProcessState.ExitCode() == 0 { |
| 87 | return fmt.Errorf("Expected Bad exit code.") |