-
Notifications
You must be signed in to change notification settings - Fork 7
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
lfcd85
wants to merge
23
commits into
master
Choose a base branch
from
kadai3-2-lfcd85
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Kadai3-2: lfcd85 #33
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
7ae88fb
kadai3-2: initial commit
lfcd85 95c864d
Implement minimum download function
lfcd85 633b622
Get URL from command argument
lfcd85 2e93c49
Add check of `Accept-Ranges` header
lfcd85 8f5dc07
Enable to split download length to ranges
lfcd85 fcd213d
Enable to download partials using goroutine
lfcd85 bc7e150
Move ranges to struct field
lfcd85 538e90d
Enable to combine partials
lfcd85 bdeb55f
Set output file name from original URL
lfcd85 f31dc80
refactor: apply go fmt
lfcd85 53cc6b2
Move partials to temporary directory
lfcd85 3fa9556
Enable to specify the number of split downloads by option
lfcd85 1d5bd63
Beautify error handling in main.go
lfcd85 7cb07cb
Remove resolved FIXME comment
lfcd85 b4fc0d9
Add check for the number of split ranges
lfcd85 75ea376
Add README
lfcd85 51b982f
Merge Execute() and download() functions
lfcd85 119efb3
Add comments for doc
lfcd85 c14e037
Edit print messages
lfcd85 d7acec5
Add minimum test for Downloader.Execute()
lfcd85 0dd26ec
Enable to test via test server
lfcd85 887f8eb
Modify after reviews
lfcd85 8f98b9c
Make errors' scopes narrower
lfcd85 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Enable to test via test server
- Loading branch information
commit 0dd26ec72115d1877f1b4edc4431cf2adce289a7
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
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) { | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. エラー処理 |
||
} |
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 @@ | ||
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. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
せっかくなんで大きなファイルもテストできるようにするとよいかも。