-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
181 additions
and
78 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package composition | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/tarent/lib-compose/logging" | ||
"os" | ||
"strings" | ||
"time" | ||
) | ||
|
||
var ResponseProcessorsNotApplicable = errors.New("request processors are not apliable on file content") | ||
|
||
type FileContentLoader struct { | ||
parser ContentParser | ||
} | ||
|
||
func NewFileContentLoader() *FileContentLoader { | ||
return &FileContentLoader{ | ||
parser: &HtmlContentParser{}, | ||
} | ||
} | ||
|
||
func (loader *FileContentLoader) Load(fd *FetchDefinition) (Content, error) { | ||
if fd.RespProc != nil { | ||
return nil, ResponseProcessorsNotApplicable | ||
} | ||
filename := strings.TrimPrefix(fd.URL, FileURLPrefix) | ||
f, err := os.Open(filename) | ||
if err != nil { | ||
return nil, fmt.Errorf("error opening file %v: %v", fd.URL, err) | ||
} | ||
|
||
c := NewMemoryContent() | ||
c.url = fd.URL | ||
c.httpStatusCode = 200 | ||
|
||
if strings.HasSuffix(filename, ".html") { | ||
parsingStart := time.Now() | ||
err := loader.parser.Parse(c, f) | ||
logging.Logger. | ||
WithField("full_url", c.URL()). | ||
WithField("duration", time.Since(parsingStart)). | ||
Debug("content parsing") | ||
f.Close() | ||
return c, err | ||
} | ||
|
||
c.reader = f | ||
return c, 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,81 @@ | ||
package composition | ||
|
||
import ( | ||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/assert" | ||
"io/ioutil" | ||
"math/rand" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func init() { | ||
rand.Seed(time.Now().UTC().UnixNano()) | ||
} | ||
|
||
func Test_FileContentLoader_LoadHTML(t *testing.T) { | ||
a := assert.New(t) | ||
|
||
fileName := filepath.Join(os.TempDir(), randString(10)+".html") | ||
|
||
err := ioutil.WriteFile(fileName, []byte("<html><head>some head content</head></html>"), 0660) | ||
a.NoError(err) | ||
|
||
loader := NewFileContentLoader() | ||
c, err := loader.Load(NewFetchDefinition(FileURLPrefix + fileName)) | ||
a.NoError(err) | ||
a.NotNil(c) | ||
a.Nil(c.Reader()) | ||
a.Equal(FileURLPrefix+fileName, c.URL()) | ||
eqFragment(t, "some head content", c.Head()) | ||
a.Equal(0, len(c.Body())) | ||
} | ||
|
||
func Test_FileContentLoader_LoadStream(t *testing.T) { | ||
a := assert.New(t) | ||
|
||
fileName := filepath.Join(os.TempDir(), randString(10)+".css") | ||
|
||
err := ioutil.WriteFile(fileName, []byte("some non html content"), 0660) | ||
a.NoError(err) | ||
|
||
loader := NewFileContentLoader() | ||
c, err := loader.Load(NewFetchDefinition(FileURLPrefix + fileName)) | ||
a.NoError(err) | ||
a.NotNil(c) | ||
body, err := ioutil.ReadAll(c.Reader()) | ||
a.NoError(err) | ||
a.Equal("some non html content", string(body)) | ||
} | ||
|
||
func Test_FileContentLoader_LoadError(t *testing.T) { | ||
a := assert.New(t) | ||
|
||
loader := NewFileContentLoader() | ||
_, err := loader.Load(NewFetchDefinition("/tmp/some/non/existing/path")) | ||
a.Error(err) | ||
} | ||
|
||
func Test_FileContentLoader_RequestProcessor(t *testing.T) { | ||
a := assert.New(t) | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
fd := NewFetchDefinition("/tmp/some/non/existing/path") | ||
fd.RespProc = NewMockResponseProcessor(ctrl) | ||
|
||
_, err := NewFileContentLoader().Load(fd) | ||
a.Equal(ResponseProcessorsNotApplicable, err) | ||
} | ||
|
||
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" | ||
|
||
func randString(n int) string { | ||
b := make([]byte, n) | ||
for i := range b { | ||
b[i] = letterBytes[rand.Intn(len(letterBytes))] | ||
} | ||
return string(b) | ||
} |
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