Skip to content

Commit 69b51fc

Browse files
authored
fix: harOpen error if no har in zip file (#473)
1 parent 3d5c7c7 commit 69b51fc

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

channel.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package playwright
22

3-
import (
4-
"reflect"
5-
)
6-
73
type channel struct {
84
eventEmitter
95
guid string
@@ -40,14 +36,11 @@ func (c *channel) innerSend(method string, returnAsDict bool, options ...interfa
4036
if returnAsDict {
4137
return result, nil
4238
}
43-
if reflect.TypeOf(result).Kind() == reflect.Map {
44-
mapV := result.(map[string]interface{})
45-
if len(mapV) == 0 {
46-
return nil, nil
47-
}
39+
if mapV, ok := result.(map[string]interface{}); ok && len(mapV) <= 1 {
4840
for key := range mapV {
4941
return mapV[key], nil
5042
}
43+
return nil, nil
5144
}
5245
return result, nil
5346
}

local_utils.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,21 @@ func (l *localUtilsImpl) Zip(options localUtilsZipOptions) (interface{}, error)
4444
}
4545

4646
func (l *localUtilsImpl) HarOpen(file string) (string, error) {
47-
harId, err := l.channel.Send("harOpen", []map[string]interface{}{
47+
result, err := l.channel.SendReturnAsDict("harOpen", []map[string]interface{}{
4848
{
4949
"file": file,
5050
},
5151
})
52-
if harId == nil {
53-
return "", err
52+
if err == nil {
53+
value := result.(map[string]interface{})
54+
if harId, ok := value["harId"]; ok {
55+
return harId.(string), nil
56+
}
57+
if err, ok := value["error"]; ok {
58+
return "", fmt.Errorf("%w:%v", ErrPlaywright, err)
59+
}
5460
}
55-
return harId.(string), err
61+
return "", err
5662
}
5763

5864
func (l *localUtilsImpl) HarLookup(option harLookupOptions) (*harLookupResult, error) {

tests/har_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,3 +786,9 @@ func TestShouldUpdateExtractedHarZipForPage(t *testing.T) {
786786
require.Contains(t, string(body), "hello, world!")
787787
require.NoError(t, expect.Locator(page2.Locator("body")).ToHaveCSS("background-color", "rgb(255, 192, 203)"))
788788
}
789+
790+
func TestShouldErrorWhenWrongZipFile(t *testing.T) {
791+
BeforeEach(t)
792+
793+
require.Error(t, page.RouteFromHAR(Asset("chromium-linux.zip")))
794+
}

0 commit comments

Comments
 (0)