Skip to content

Commit

Permalink
Merge pull request exercism#793 from jdsutherland/submit-handles-non2xx
Browse files Browse the repository at this point in the history
Submit handles non 2xx responses
  • Loading branch information
Katrina Owen authored Mar 8, 2019
2 parents 1d02944 + 10aea95 commit 89e80c4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
3 changes: 1 addition & 2 deletions cmd/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"

Expand Down Expand Up @@ -268,7 +267,7 @@ func (s *submitCmdContext) submit(metadata *workspace.ExerciseMetadata, docs []w
}
defer resp.Body.Close()

if resp.StatusCode == http.StatusBadRequest {
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return decodedAPIError(resp)
}

Expand Down
38 changes: 38 additions & 0 deletions cmd/submit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,44 @@ func TestSubmitServerErr(t *testing.T) {
assert.Regexp(t, "test error", err.Error())
}

func TestHandleErrorResponse(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
})

ts := httptest.NewServer(handler)
defer ts.Close()

tmpDir, err := ioutil.TempDir("", "submit-nonsuccess")
defer os.RemoveAll(tmpDir)
assert.NoError(t, err)

v := viper.New()
v.Set("token", "abc123")
v.Set("workspace", tmpDir)
v.Set("apibaseurl", ts.URL)

cfg := config.Config{
Persister: config.InMemoryPersister{},
UserViperConfig: v,
DefaultBaseURL: "http://example.com",
}

dir := filepath.Join(tmpDir, "bogus-track", "bogus-exercise")
os.MkdirAll(filepath.Join(dir, "subdir"), os.FileMode(0755))
writeFakeMetadata(t, dir, "bogus-track", "bogus-exercise")

err = ioutil.WriteFile(filepath.Join(dir, "file-1.txt"), []byte("This is file 1"), os.FileMode(0755))
assert.NoError(t, err)

files := []string{
filepath.Join(dir, "file-1.txt"),
}

err = runSubmit(cfg, pflag.NewFlagSet("fake", pflag.PanicOnError), files)
assert.Error(t, err)
}

func TestSubmissionNotConnectedToRequesterAccount(t *testing.T) {
submittedFiles := map[string]string{}
ts := fakeSubmitServer(t, submittedFiles)
Expand Down

0 comments on commit 89e80c4

Please sign in to comment.