-
Notifications
You must be signed in to change notification settings - Fork 22
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
8 changed files
with
137 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,21 @@ | ||
function generateEndHex(length, status) { | ||
// This should come from the official SDK. | ||
// But there is no official SDK yet, so we keep it here. | ||
function respond(res, status) { | ||
var buffer = new ArrayBuffer(8); | ||
var view = new DataView(buffer); | ||
|
||
view.setUint32(0, status, true); | ||
view.setUint32(4, length, true); | ||
|
||
var hexString = ""; | ||
for (var i = 0; i < buffer.byteLength; i++) { | ||
var hex = view.getUint8(i).toString(16); | ||
hexString += hex.padStart(2, "0"); | ||
} | ||
view.setUint32(4, res.length, true); | ||
|
||
return hexString; | ||
var bytes = new Uint8Array(buffer); | ||
print(res) | ||
print(bytes) | ||
} | ||
|
||
// This should come from the official SDK. | ||
// But there is no official SDK yet, so we keep it here. | ||
function respond(res, status) { | ||
putstr(res+generateEndHex(res.length, status)) | ||
} | ||
console.log("user log here") | ||
console.log("user log here") | ||
console.log("user log here") | ||
console.log("user log here") | ||
console.log("user log here") | ||
|
||
respond("<h1>From my Raptor application</h1></br>some other stuff here</br>", 200) |
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 |
---|---|---|
@@ -1,27 +1,104 @@ | ||
package shared | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"encoding/hex" | ||
"fmt" | ||
"log" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func BenchmarkParseStdout(b *testing.B) { | ||
userResp := "<h1>This is the actual response</h1>" | ||
statusCode := uint32(200) | ||
userLogs := ` | ||
the big brown fox | ||
the big brown fox | ||
the big brown fox | ||
the big brown fox | ||
the big brown fox | ||
` | ||
builder := &bytes.Buffer{} | ||
|
||
buf := make([]byte, 8) | ||
binary.LittleEndian.PutUint32(buf[0:4], statusCode) | ||
binary.LittleEndian.PutUint32(buf[4:8], uint32(len(userResp))) | ||
|
||
b.ResetTimer() | ||
b.StartTimer() | ||
b.ReportAllocs() | ||
for i := 0; i < b.N; i++ { | ||
builder.WriteString(userLogs) | ||
builder.WriteString(userResp) | ||
builder.Write(buf) | ||
if _, _, _, err := ParseStdout(builder); err != nil { | ||
log.Fatal(err) | ||
} | ||
builder.Reset() | ||
} | ||
b.StopTimer() | ||
} | ||
|
||
func TestParseWithoutUserLogs(t *testing.T) { | ||
userResp := "<h1>This is the actual response</h1>" | ||
statusCode := uint32(200) | ||
builder := &bytes.Buffer{} | ||
builder.WriteString(userResp) | ||
|
||
// += userResp | ||
buf := make([]byte, 8) | ||
binary.LittleEndian.PutUint32(buf[0:4], statusCode) | ||
binary.LittleEndian.PutUint32(buf[4:8], uint32(len(userResp))) | ||
builder.Write(buf) | ||
|
||
logs, resp, status, err := ParseStdout(builder) | ||
require.Nil(t, err) | ||
require.Equal(t, int(statusCode), status) | ||
require.Equal(t, userResp, string(resp)) | ||
require.Equal(t, []byte{}, logs) | ||
} | ||
|
||
func TestParseWithUserLogs(t *testing.T) { | ||
userResp := "<h1>This is the actual response</h1>" | ||
statusCode := uint32(200) | ||
userLogs := ` | ||
the big brown fox | ||
the big brown fox | ||
the big brown fox | ||
the big brown fox | ||
the big brown fox | ||
` | ||
builder := &bytes.Buffer{} | ||
builder.WriteString(userLogs) | ||
builder.WriteString(userResp) | ||
|
||
// += userResp | ||
buf := make([]byte, 8) | ||
binary.LittleEndian.PutUint32(buf[0:4], statusCode) | ||
binary.LittleEndian.PutUint32(buf[4:8], uint32(len(userResp))) | ||
builder.Write(buf) | ||
|
||
logs, resp, status, err := ParseStdout(builder) | ||
require.Nil(t, err) | ||
require.Equal(t, int(statusCode), status) | ||
require.Equal(t, userResp, string(resp)) | ||
require.Equal(t, userLogs, string(logs)) | ||
} | ||
|
||
func TestParseRuntimeHTTPResponse(t *testing.T) { | ||
t.Run("multiline response", func(t *testing.T) { | ||
text := "This is the best.\nBut not always correct.\nThe big brown fox." | ||
statusCode := uint32(500) | ||
|
||
buf := make([]byte, 8) | ||
binary.LittleEndian.PutUint32(buf[0:4], statusCode) | ||
binary.LittleEndian.PutUint32(buf[4:8], uint32(len(text))) | ||
|
||
in := fmt.Sprintf("%s%s", text, hex.EncodeToString(buf)) | ||
resp, status, err := ParseRuntimeHTTPResponse(in) | ||
require.Nil(t, err) | ||
require.Equal(t, int(statusCode), status) | ||
require.Equal(t, text, resp) | ||
}) | ||
text := "This is the best.\nBut not always correct.\nThe big brown fox." | ||
statusCode := uint32(500) | ||
|
||
buf := make([]byte, 8) | ||
binary.LittleEndian.PutUint32(buf[0:4], statusCode) | ||
binary.LittleEndian.PutUint32(buf[4:8], uint32(len(text))) | ||
|
||
in := fmt.Sprintf("%s%s", text, hex.EncodeToString(buf)) | ||
resp, status, err := ParseRuntimeHTTPResponse(in) | ||
require.Nil(t, err) | ||
require.Equal(t, int(statusCode), status) | ||
require.Equal(t, text, resp) | ||
} |
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