Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kadai3-2: lfcd85 #33

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Enable to test via test server
  • Loading branch information
lfcd85 committed Jun 21, 2019
commit 0dd26ec72115d1877f1b4edc4431cf2adce289a7
77 changes: 72 additions & 5 deletions kadai3-2/lfcd85/mypget/mypget_test.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
package mypget_test

import (
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"os"
"strconv"
"strings"
"testing"

"github.com/gopherdojo/dojo5/kadai3-2/lfcd85/mypget"
)

type testServerFile struct {
testDataPath string
}

func TestDownloader_Execute(t *testing.T) {
cases := []struct {
urlStr string
splitNum int
testDataPath string
splitNum int
}{
{"https://golang.org/doc/gopher/frontpage.png", 8}, // TODO: replace URL with local test server
{"../testdata/tower.jpg", 8},
{"../testdata/lorem_ipsum.txt", 4},
}

for _, c := range cases {
c := c
t.Run(c.urlStr, func(t *testing.T) {
d := initDownloader(t, c.urlStr, c.splitNum)
t.Run(c.testDataPath, func(t *testing.T) {
tsf := testServerFile{c.testDataPath}

ts, closeTs := initTestServer(t, tsf.testServerHandler)
defer closeTs()

d := initDownloader(t, ts.URL, c.splitNum)
if err := d.Execute(); err != nil {
t.Errorf("failed to execute split downloader: %v", err)
}
@@ -50,3 +65,55 @@ func deleteOutputFile(t *testing.T, d *mypget.Downloader) {
}
}
}

func initTestServer(t *testing.T, handler func(t *testing.T, w http.ResponseWriter, r *http.Request)) (*httptest.Server, func()) {
t.Helper()

ts := httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
handler(t, w, r)
},
))

return ts, func() { ts.Close() }
}

func (tsf *testServerFile) testServerHandler(t *testing.T, w http.ResponseWriter, r *http.Request) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

せっかくなんで大きなファイルもテストできるようにするとよいかも。

t.Helper()

w.Header().Set("Accept-Ranges", "bytes")
headerRange := r.Header.Get("Range")

body := func() []byte {
testDataBytes, err := ioutil.ReadFile(tsf.testDataPath)
if err != nil {
t.Errorf("failed to read the test file in test server: %v", err)
}

if headerRange == "" {
return testDataBytes
}

rangeItems := strings.Split(headerRange, "=")
if rangeItems[0] != "bytes" {
t.Errorf("range in test server should have bytes value, but actually does not")
}
rangeValues := strings.Split(rangeItems[1], "-")

rangeStart, err := strconv.Atoi(rangeValues[0])
if err != nil {
t.Errorf("failed to get the start of the range in test server: %v", err)
}

rangeEnd, err := strconv.Atoi(rangeValues[1])
if err != nil {
t.Errorf("failed to get the end of the range in test server: %v", err)
}

return testDataBytes[rangeStart:rangeEnd]
}()

w.Header().Set("Content-Length", strconv.Itoa(len(body)))
w.WriteHeader(http.StatusPartialContent)
w.Write(body)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

エラー処理

}
1 change: 1 addition & 0 deletions kadai3-2/lfcd85/testdata/lorem_ipsum.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Binary file added kadai3-2/lfcd85/testdata/tower.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.