Merge pull request #2755 from thaJeztah/dont_save_useragent

Config-file: remove User-Agent from config.json when saving
This commit is contained in:
Sebastiaan van Stijn 2020-10-15 22:17:59 +02:00 committed by GitHub
commit 9b3eef5218
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 1 deletions

View File

@ -169,6 +169,13 @@ func (configFile *ConfigFile) SaveToWriter(writer io.Writer) error {
configFile.AuthConfigs = tmpAuthConfigs configFile.AuthConfigs = tmpAuthConfigs
defer func() { configFile.AuthConfigs = saveAuthConfigs }() defer func() { configFile.AuthConfigs = saveAuthConfigs }()
// User-Agent header is automatically set, and should not be stored in the configuration
for v := range configFile.HTTPHeaders {
if strings.EqualFold(v, "User-Agent") {
delete(configFile.HTTPHeaders, v)
}
}
data, err := json.MarshalIndent(configFile, "", "\t") data, err := json.MarshalIndent(configFile, "", "\t")
if err != nil { if err != nil {
return err return err

View File

@ -466,7 +466,27 @@ func TestSave(t *testing.T) {
assert.NilError(t, err) assert.NilError(t, err)
cfg, err := ioutil.ReadFile("test-save") cfg, err := ioutil.ReadFile("test-save")
assert.NilError(t, err) assert.NilError(t, err)
assert.Check(t, is.Equal(string(cfg), "{\n \"auths\": {}\n}")) assert.Equal(t, string(cfg), `{
"auths": {}
}`)
}
func TestSaveCustomHTTPHeaders(t *testing.T) {
configFile := New(t.Name())
defer os.Remove(t.Name())
configFile.HTTPHeaders["CUSTOM-HEADER"] = "custom-value"
configFile.HTTPHeaders["User-Agent"] = "user-agent 1"
configFile.HTTPHeaders["user-agent"] = "user-agent 2"
err := configFile.Save()
assert.NilError(t, err)
cfg, err := ioutil.ReadFile(t.Name())
assert.NilError(t, err)
assert.Equal(t, string(cfg), `{
"auths": {},
"HttpHeaders": {
"CUSTOM-HEADER": "custom-value"
}
}`)
} }
func TestSaveWithSymlink(t *testing.T) { func TestSaveWithSymlink(t *testing.T) {