Skip to content
This repository was archived by the owner on Jul 12, 2025. It is now read-only.

Commit 1c428f0

Browse files
authored
Merge pull request #865 from aisk/check-http-method
Add Net::HTTP methods check
2 parents dd22492 + c3d0ae4 commit 1c428f0

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

vm/http.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ var builtinHTTPClassMethods = []*BuiltinMethodObject{
2828
// Sends a GET request to the target and returns the HTTP response as a string. Will error on non-200 responses, for more control over http requests look at the `start` method.
2929
Name: "get",
3030
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
31+
if len(args) < 1 {
32+
return t.vm.InitErrorObject(errors.ArgumentError, sourceLine, errors.WrongNumberOfArgumentMore, 1, len(args))
33+
}
34+
3135
arg0, ok := args[0].(*StringObject)
3236
if !ok {
3337
return t.vm.InitErrorObject(errors.ArgumentError, sourceLine, errors.WrongArgumentTypeFormatNum, 0, "String", args[0].Class().Name)
@@ -115,6 +119,10 @@ var builtinHTTPClassMethods = []*BuiltinMethodObject{
115119
// Sends a HEAD request to the target with type header and body. Returns the HTTP headers as a map[string]string. Will error on non-200 responses, for more control over http requests look at the `start` method.
116120
Name: "head",
117121
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
122+
if len(args) < 1 {
123+
return t.vm.InitErrorObject(errors.ArgumentError, sourceLine, errors.WrongNumberOfArgumentMore, 1, len(args))
124+
}
125+
118126
arg0, ok := args[0].(*StringObject)
119127
if !ok {
120128
return t.vm.InitErrorObject(errors.ArgumentError, sourceLine, errors.WrongArgumentTypeFormatNum, 0, "String", args[0].Class().Name)

vm/http_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ func TestHTTPRequestFail(t *testing.T) {
7777
7878
Net::HTTP.get(42)
7979
`, "ArgumentError: Expect argument #0 to be String. got: Integer", 1},
80+
//Zero argument for get()
81+
{`
82+
require "net/http"
83+
84+
Net::HTTP.get
85+
`, "ArgumentError: Expect 1 or more argument(s). got: 0", 1},
86+
//Zero argument for head()
87+
{`
88+
require "net/http"
89+
90+
Net::HTTP.head
91+
`, "ArgumentError: Expect 1 or more argument(s). got: 0", 1},
8092
{`
8193
require "net/http"
8294

0 commit comments

Comments
 (0)