Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HELP]: questions in desc #148

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

[HELP]: questions in desc #148

wants to merge 1 commit into from

Conversation

serranoio
Copy link
Contributor

@serranoio serranoio commented Feb 17, 2025

mockMode does not work on redirected paths -

  1. I needed paths to be redirected before requests are checked, so I took the functionality out of callApi() and put it on lines 143-144 in handle_request.go. This does not seem like good code, but I don't know if this breaks anything or not since there's no tests :(. But hey, it works and the tests pass. Is this ok?
  2. For some reason, auth is dropped from the original header in my request when it comes FROM THE BROWSER - mockMode works when curling the endpoint with authentication added as headers, but when the request comes from the browser, it fails. So on line 145 in handle_request.go I added the header back and it works but this seems like a hack. Am I missing something? What am I missing? I cannot tell since there are no tests :(
  3. Wiretap really needs tests. I really want to write a test like in wireap_service_test.go, but I just dont know how to get it to work(this test is commented out to show you that my changes to wiretap in 1.,2., work). Can you help me formulate the best testing strategy moving forward? What do I need to learn? I am seriously willing to beef wiretap tf up. It's going to need it after my planned changes (which I havent mentioned yet)

PS: extra changes which dont need review yet but I will describe them if you are curious-
Screenshot 2025-02-17 at 10 43 05 AM

  1. requests made in mockMode now show up in the UI as mockMode.
Screenshot 2025-02-17 at 10 44 49 AM 2. I added a quick keyword filter at the top. Wanted to quickly search the httpTransactions.

mockMode does not work on redirected paths -
1.  I needed paths to be redirected before requests are checked, so I took the functionality out of callApi() and put it on lines 143-144 in `handle_request.go`. This does not seem like good code, but I dont know if this breaks anything or not since there's no tests :(. But hey, it works and the tests pass. Is this ok?
2. For some reason, auth is dropped from the original header in my request when it comes FROM THE BROWSER - MockMode works when curling the endpoint with authentication added as headers, but when the request comes from the browser, it fails. So on line 145 in `handle_request.go` I added the header back, but this seems like a hack. Am I missing something? What am I missing? I cannot tell since there are no tests :(

3. Wiretap really needs tests. I really want to write a test like in `wireap_service_test.go`, but I just dont know how to get it to work. Any pointers? (this test is commented out to show you that my changes to wiretap in 1.,2., work)
Copy link
Member

@daveshanley daveshanley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR needs to broken down, I really don't know why the UI has been modified.

Can you remove the UI code? I am planning on stripping out 99% of the UI from wiretap and moving it to another repo - if you want to add UI changes, please do so in another PR so it's a composed set of PRs that build on top of each other.

Also, I don't think you have used gofmt as there are a bunch of whitespace changes.


// create a new request from the original request, but replace the path
wiretapConfig := configStore.(*shared.WiretapConfiguration)
func (ws *WiretapService) rewritingPath(req *http.Request, wiretapConfig *shared.WiretapConfiguration) string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rename this to rewritingPath to checkForPathRewrite

@@ -58,6 +59,10 @@ func (ws *WiretapService) handleMockRequest(
}
}

const mocked = "this response has been mocked"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a place holder for the future. Eventually I want to return to the UI what was used for mocking (examples, example, schema) and if there is a name, the name as well

@@ -58,6 +59,10 @@ func (ws *WiretapService) handleMockRequest(
}
}

const mocked = "this response has been mocked"
request.HttpResponseWriter.Header().Set("wiretap-mock", fmt.Sprintf(mocked))
header.Add("wiretap-mock", fmt.Sprintf(mocked))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this? why are we adding custom headers?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted the UI to be able to differentiate between mocked data & non-mocked data. Since this is a 'simulated response to send up to the monitor UI, I thought that I could simply add wiretap specific data to the header

ws.config.Logger.Info("MockMode enabled; skipping validation")
ws.rewritingPath(request.HttpRequest, config)
ws.rewritingPath(newReq, config)
request.HttpRequest.Header.Set("Authorization", apiRequest.Header.Get("Authorization"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the wrong place to do this. I think there is something missing during the request cloning process https://github.com/pb33f/wiretap/blob/main/daemon/clone_request.go

Every request is cloned before being parsed because you can only ready the body once, therefore to allow the chain to continue, each time you read the body, you need to replace it with a fresh stream, because its dead.

I investigate what the browser is sending first and then try to fix this, because if the header is being set correctly via a cURL command, but failing with the browser - there is a failure to parse the header the browser is sending..

Start there, because this is a hack and we can't do this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the advice, thats what I thought

@@ -0,0 +1,122 @@
package daemon

// this test does not work. I have no idea how to test the platform. It will take a decent amount of work to dig deep.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to test the platform, you need to test the logic, all can be mocked using the standard library.

mockedhttps://www.cloudbees.com/blog/testing-http-handlers-go

// https://swagger.io/docs/specification/v3_0/data-models/oneof-anyof-allof-not/
// anyOf, oneOf, allOf, not
// only supports v3 model, f the rest LOL
func (rme *ResponseMockEngine) GetSchemaCombination(mediaType *v3.MediaType) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what this is, delete please unless we need it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah so sorry. I did not mean for you to review the MR. This function was not finished. I only inteded for help.

The mockGenerator does not support anyOf, oneOf, allOf, not. I was going to implement it. The use case is that there exists different shapes of data that are returned from a singular endpoint (bad api design)

@daveshanley
Copy link
Member

mockMode does not work on redirected paths -

  1. I needed paths to be redirected before requests are checked, so I took the functionality out of callApi() and put it on lines 143-144 in handle_request.go. This does not seem like good code, but I don't know if this breaks anything or not since there's no tests :(. But hey, it works and the tests pass. Is this ok?

Why does it seem like bad code? All you did was shift logic around a little? The code is the same from what I can tell, you just moved things around a little?

  1. For some reason, auth is dropped from the original header in my request when it comes FROM THE BROWSER - mockMode works when curling the endpoint with authentication added as headers, but when the request comes from the browser, it fails. So on line 145 in handle_request.go I added the header back and it works but this seems like a hack. Am I missing something? What am I missing? I cannot tell since there are no tests :(

I left a comment about this, I would investigate the request cloning process first and check out exactly what is going on with the browser sending headers that cannot be parsed, because if a cURL command works, but a browser request does not - then there is a major issue with parsing the header correctly, - because the logic is working.

  1. Wiretap really needs tests. I really want to write a test like in wireap_service_test.go, but I just dont know how to get it to work(this test is commented out to show you that my changes to wiretap in 1.,2., work). Can you help me formulate the best testing strategy moving forward? What do I need to learn? I am seriously willing to beef wiretap tf up. It's going to need it after my planned changes (which I havent mentioned yet)

I think you need to take a step back and learn about http mock testing in go. You don't need to run the whole platform, you just need to use the standard library for testing HTTP, it's got a rich set of APIs for this.

https://github.com/pb33f/wiretap/blob/main/mock/mock_engine_test.go

There are limited tests in wiretap because the vast majority of logic lives outside this tool, it lives libopenapi and libopenapi-validator wiretap just adds a little control logic on top of it.

PS: extra changes which dont need review yet but I will describe them if you are curious- Screenshot 2025-02-17 at 10 43 05 AM

  1. requests made in mockMode now show up in the UI as mockMode.

Screenshot 2025-02-17 at 10 44 49 AM 2. I added a quick keyword filter at the top. Wanted to quickly search the httpTransactions.

I would really like to discuss the UI work first it, I am extremely fussy in this area :), it should come in as another PR.

mockMode does not work on redirected paths -

  1. I needed paths to be redirected before requests are checked, so I took the functionality out of callApi() and put it on lines 143-144 in handle_request.go. This does not seem like good code, but I don't know if this breaks anything or not since there's no tests :(. But hey, it works and the tests pass. Is this ok?
  2. For some reason, auth is dropped from the original header in my request when it comes FROM THE BROWSER - mockMode works when curling the endpoint with authentication added as headers, but when the request comes from the browser, it fails. So on line 145 in handle_request.go I added the header back and it works but this seems like a hack. Am I missing something? What am I missing? I cannot tell since there are no tests :(
  3. Wiretap really needs tests. I really want to write a test like in wireap_service_test.go, but I just dont know how to get it to work(this test is commented out to show you that my changes to wiretap in 1.,2., work). Can you help me formulate the best testing strategy moving forward? What do I need to learn? I am seriously willing to beef wiretap tf up. It's going to need it after my planned changes (which I havent mentioned yet)

PS: extra changes which dont need review yet but I will describe them if you are curious- Screenshot 2025-02-17 at 10 43 05 AM

  1. requests made in mockMode now show up in the UI as mockMode.

Screenshot 2025-02-17 at 10 44 49 AM 2. I added a quick keyword filter at the top. Wanted to quickly search the httpTransactions.

@serranoio
Copy link
Contributor Author

Hey, sorry! I forgot to mark this as a DRAFT mr. My intentions were to ask for help - code is not ready which is why the code looks like shit. Sorry about that 🙏

@serranoio
Copy link
Contributor Author

Why does it seem like bad code? All you did was shift logic around a little? The code is the same from what I can tell, you just moved things around a little?

I am rewriting the path only for mockMode. I feel like we should only have to rewrite a path once for all of the cases.
But tbh, I dont have a clue, as I wouldn't know the blast radius of the change since theres no tests.

I left a comment about this, I would investigate the request cloning process first and check out exactly what is going on with the browser sending headers that cannot be parsed, because if a cURL command works, but a browser request does not - then there is a major issue with parsing the header correctly, - because the logic is working.
💯 💯 yes, thank you. This is a big help.

I think you need to take a step back and learn about http mock testing in go. You don't need to run the whole platform, you just need to use the standard library for testing HTTP, it's got a rich set of APIs for this.

Thank you, that makes sense. Will take a step back 🙏

https://github.com/pb33f/wiretap/blob/main/mock/mock_engine_test.go

There are limited tests in wiretap because the vast majority of logic lives outside this tool, it lives libopenapi and libopenapi-validator wiretap just adds a little control logic on top of it.
👍 💯

I would really like to discuss the UI work first it, I am extremely fussy in this area :), it should come in as another PR.

100% will put them in another PR. I do have several more changes - 2 new services planned. I'll propose them in issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants