mapping function to html route in crow - c++

I am going through C++ crow webserver framework, here example is given as below
CROW_ROUTE(app, url)
Example is given below as
CROW_ROUTE(app, "/add/<int>/<int>")
([](int a, int b)
{
return std::to_string(a+b);
});
Lambda function below is not passed to CROW_ROUTE. My question is how CROW framework knows that /add// maps to lamdba function below. How this is done?

Related

Mock extensions function inside Companion Object

I have a class (class A) to which I define an extension function (A.extension()) inside a companion object of another class (class B) for a matter of organization.
On my tests I need:
To use a real class A instance .
To mock A.extension().
To use a mock instance of class B.
Using MockK-library I am not being able to mock that extension function successfully.
I've tried:
mockkObject(B.Companion) {
every { any<A>().extension() } returns whatIneed
}
result: Tries to run the unmocked version of the extension function.
mockkStatic(path.to.B.CompanionKt)
every { any<A>().extension() } returns whatIneed
Result: It does not find the Companion Object.
mockkStatic(A::extension) {
every { any<A>().extension() } returns whatIneed
}
Result: Compile error -> 'extension' is a member and an extension at the same time. References to such elements are not allowed.
Am I missing something regarding how to mock this ?
Am I doing something wrong in terms of code structuring that prevents this mocking to be possible?
Any help is appreciated.
This seems to be an impossible thing. I have tried this severally and it does not work.

How to unit test go code that interacts with Elasticsearch

I have an application that defines a type Client struct {} which talks to various other clients in my code that talk to services like github, elasticsearch etc.
Now I have the following ES code in one of my packages
type SinkService interface {
Write(context, index, mapping, doc)
}
type ESSink struct {
client *elastic.Client
}
func NewESSink() *ESSink {}
// checks if the index exists and writes the doc
func (s *ESSink) Write(context, index, mapping, doc) {}
I use this method in my main client that runs the whole application like this c.es.Write(...). Now if I want to write client_test.go I can simply make a mockESSink and use it with some stub code but that won't cover the lines written in my ES code.
How do I unit test my ES code? My ESSink uses an elastic.Client. How do I mock that?
I would like to embed some mock ES client that gives me stub responses and I will be able to test my ESSink.Write method that way.
Based on your question, I assume you're using github.com/olivere/elastic, and you want to be able to test by using stub http responses. When I first read this question, I also have never written Go test code that use ES client. So, in addition to answering this question, I'm also sharing how I find out the answer from the godocs.
First, we can see that elastic.NewClient accepts client option functions. So I checked what kind of client option functions the library provides. Turns out the library provides elastic.SetHttpClient that accepts elastic.Doer. The Doer is an interface that http.Client can implement. From here, the answer becomes clear.
So, you have to:
Change your func NewESSink() to accept http Client or elastic Client.
Write stub http Client (implements elastic.Doer).
ESSink
type ESSink struct {
client *elastic.Client
}
func NewESSink(client *elastic.Client) *ESSink {
return &ESSink{client: client}
}
Stub HttpClient
package stubs
import "net/http"
type HTTPClient struct {
Response *http.Response
Error error
}
func (c *HTTPClient) Do(*http.Request) (*http.Response, error) {
return c.Response, c.Error
}
Your testing code
func TestWrite(t *testing.T) {
// set the body and error according to your test case
stubHttpClient := stubs.HTTPClient{
Response: &http.Response{Body: ...},
Error: ...,
}
elasticClient := elastic.NewClient(elastic.SetHttpClient(stubHttpClient))
esSink := NewESSink(elasticClient)
esSink.Write(...)
}
In your production code, you can use http.Client{} when setting ES http client.

How to address NodeJS's built-in functions with Sinon?

Using Sinon stubs, I have successfully stubbed functions along the project, using the traditional stubbing syntax:
const permissionsStub = sinon.stub(invitation, 'update')
sinon.assert.calledOnce(permissionsStub)
Now I am trying to Stub a built-in function of NodeJS (findIndex, indexOf, etc..), without success, as it should be passed with the in the first argument of Sinon's stub() function call, and as that is a NodeJS's core function, what needs to be passed?
I have tried creating an empty stub and then assigning it directly to the function in the code, but I think there is a simpler way to try and spy/stub/mock built-in NodeJS functions.
I get this error by trying to pass a few arguments that might target it correctly, as it doesn't succeed mounting:
TypeError: Cannot read property 'returns' of undefined
How can I target those functions in a simpler way using Chai and Sinon?
So eventually I got it to work using the following code (rewire is being used to get to the inner unexported functions of the file):
const permissionIndex = invitation.__get__('permissionIndex')
permissionsStub.findIndex = () => {
return { id: 1 }
}
expect(permissionIndex(1, permissionsStub)).to.be.an('object')
expect(permissionIndex(1, permissionsStub)).to.have.deep.property('id', 1)

Can a C++ addon in Node.js override some V8 class or method?

I'm not a C++ programmer, so please forgive me if this is a stupid question.
Suppose that I want to change a particular behavior in V8. Of course I can change it inside deps/v8 and then build Node.js from source. But I'm searching if there is an easier way.
Can I write a C++ addon to override what I want in V8?
For example, could I create an empty addon:
namespace MY_EMPTY_ADDON
{
void init(v8::Local<v8::Object> exports, v8::Local<v8::Object> module)
{
// empty
}
NODE_MODULE(MY_EMPTY_ADDON, init)
}
Then, at the same file add some thing like this:
namespace v8
{
namespace internal
{
// here I do my changes
}
}
Could this work?
No. C++ does not allow monkey-patching in the way JavaScript does. You cannot override another module's/library's functions from the outside.

how to pass parameter to WebService in Spring DSL

I am new to apache Camel. I am trying to invoke a simple calculator WebService which I have created netbeans using Metro JAXWS . The Calculator has one webmethod with following signature:
int add int a, int b
Now, my goal is to invoke this webservice using Apache camel, cxf component. After reading multiple tutorials and examples, I completed following steps:
1) Used wsdl2java tool to generate WebService stub in the project.
2) Defined CXFEndpoint in camel-cxf.xml.
<cxf:cxfEndpoint id="calEndPoint"`
address="http://localhost:8080/Calculator/Calculator"
serviceClass="com.test.calculatorbeanws.Calculator"
wsdlURL="wsdl/calculator.wsdl"/>
3) Now, I am trying to create a route in camel-context.xml to call this web-service. It should look something like this.
<camel:route>
<camel:from ....." />
<camel:to uri="cxf:bean:calEndPoint"/>
<camel:to uri="file:someoutputfile"/>
</camel:route>
So, the problem is defining this route. How can I pass parameters a and b to calEndPoint, and how can I write the output to some output file or display it on console using Spring DSL.
Hopefully, I have explained the problems in detail. If anybody has any suggestion, please let me know.
Thanks