-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from friendsofgo/config_file
Allow to run mock server via config file
- Loading branch information
Showing
12 changed files
with
270 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package killgrave | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
|
||
"github.com/pkg/errors" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
// Config representation of config file yaml | ||
type Config struct { | ||
ImpostersPath string `yaml:"imposters_path"` | ||
Port int `yaml:"port"` | ||
Host string `yaml:"host"` | ||
CORS ConfigCORS `yaml:"cors"` | ||
} | ||
|
||
// ConfigCORS representation of section CORS of the yaml | ||
type ConfigCORS struct { | ||
Methods []string `yaml:"methods"` | ||
Headers []string `yaml:"headers"` | ||
Origins []string `yaml:"origins"` | ||
ExposedHeaders []string `yaml:"exposed_headers"` | ||
AllowCredentials bool `yaml:"allow_credentials"` | ||
} | ||
|
||
// ReadConfigFile unmarshal content of config file to Config struct | ||
func ReadConfigFile(path string, config *Config) error { | ||
configFile, err := os.Open(path) | ||
if err != nil { | ||
return errors.Wrapf(err, "error trying to read config file: %s", path) | ||
} | ||
defer configFile.Close() | ||
|
||
bytes, _ := ioutil.ReadAll(configFile) | ||
if err := yaml.Unmarshal(bytes, config); err != nil { | ||
return errors.Wrapf(err, "error while unmarshall configFile file %s", path) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package killgrave | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
func TestReadConfigFile(t *testing.T) { | ||
tests := map[string]struct { | ||
input string | ||
expected Config | ||
err error | ||
}{ | ||
"valid config file": {"test/testdata/config.yml", validConfig(), nil}, | ||
"file not found": {"test/testdata/file.yml", Config{}, errors.New("error")}, | ||
"wrong yaml file": {"test/testdata/wrong_config.yml", Config{}, errors.New("error")}, | ||
} | ||
|
||
for name, tc := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
var got Config | ||
err := ReadConfigFile(tc.input, &got) | ||
|
||
if err != nil && tc.err == nil { | ||
t.Fatalf("not expected any erros and got %v", err) | ||
} | ||
|
||
if err == nil && tc.err != nil { | ||
t.Fatalf("expected an error and got nil") | ||
} | ||
|
||
if !reflect.DeepEqual(tc.expected, got) { | ||
t.Fatalf("expected: %v, got: %v", tc.expected, got) | ||
} | ||
|
||
}) | ||
} | ||
} | ||
|
||
func validConfig() Config { | ||
return Config{ | ||
ImpostersPath: "imposters", | ||
Port: 3000, | ||
Host: "localhost", | ||
CORS: ConfigCORS{ | ||
Methods: []string{"GET"}, | ||
Origins: []string{"*"}, | ||
Headers: []string{"Content-Type"}, | ||
ExposedHeaders: []string{"Cache-Control"}, | ||
AllowCredentials: true, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.