Skip to content

Commit

Permalink
finagle-mux: Add API for Request Which Only Needs the Body
Browse files Browse the repository at this point in the history
Problem / Solution

When testing functionality via the Mux protocol, if only testing the
internal mechanics of how clients and servers interact, specifying a
`Path` for a mux `Request` to use is unnecessary and adds to extra code
to write and maintain. This change provides the ability to create a mux
`Request` in both Scala and Java without specifying a `Path` explicitly.
In those cases `Path.empty` will be used.

Differential Revision: https://phabricator.twitter.biz/D613686
  • Loading branch information
ryanoneill authored and jenkins committed Feb 8, 2021
1 parent 6b79926 commit 3ca4630
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ New Features
Tying a `Name` directly to a `Service` can be extremely useful for testing the functionality
of a Finagle client. ``PHAB_ID=D605745``

* finagle-mux: Added variant of `c.t.f.mux.Request.apply` and `c.t.f.mux.Requests.make` which takes
only the body of the `Request` (in the form of `c.t.io.Buf`) as a parameter. This is useful for
when the path value of a `Request` is not used by the server (e.g. testing). ``PHAB_ID=D613686``

Runtime Behavior Changes
~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
12 changes: 12 additions & 0 deletions finagle-mux/src/main/scala/com/twitter/finagle/mux/Request.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ object Request {

val empty: Request = Impl(Path.empty, Nil, Buf.Empty)

/**
* Variant to create a [[Request]] which only requires a 'payload' (i.e. [[Request]] 'body'). This
* version should likely only be used when testing.
*/
def apply(payload: Buf): Request = apply(Path.empty, Nil, payload)

def apply(dst: Path, payload: Buf): Request = apply(dst, Nil, payload)

def apply(dst: Path, ctxts: Seq[(Buf, Buf)], payload: Buf): Request = Impl(dst, ctxts, payload)
Expand All @@ -33,6 +39,12 @@ object Request {
object Requests {
val empty: Request = Request.empty

/**
* Variant to create a [[Request]] which only requires a 'payload' (i.e. [[Request]] 'body'). This
* version should likely only be used when testing.
*/
def make(payload: Buf): Request = make(Path.empty, Nil, payload)

def make(dst: Path, payload: Buf): Request = make(dst, Nil, payload)

def make(dst: Path, contexts: Seq[(Buf, Buf)], payload: Buf): Request =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.twitter.finagle.mux;

import org.junit.Test;

import com.twitter.io.Buf;
import com.twitter.io.Bufs;

public class RequestsCompilationTest {

private Buf buf = Bufs.EMPTY;

@Test
public void makeWithPayload() {
Request request = Requests.make(buf);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.twitter.finagle.mux

import com.twitter.finagle.Path
import com.twitter.io.Buf
import org.scalatest.FunSuite

class RequestTest extends FunSuite {

test("create request with payload") {
val buf = Buf.Utf8("Hello")
val request = Request(buf)
assert(request.destination == Path.empty)
assert(request.contexts == Nil)
assert(request.body == buf)
}

}

0 comments on commit 3ca4630

Please sign in to comment.