-
Notifications
You must be signed in to change notification settings - Fork 0
URL-encode branch refs in DevPullCommand GitHub API requests #20
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Notur\Tests\Unit\Console; | ||
|
|
||
| use GuzzleHttp\Client; | ||
| use GuzzleHttp\Handler\MockHandler; | ||
| use GuzzleHttp\HandlerStack; | ||
| use GuzzleHttp\Middleware; | ||
| use GuzzleHttp\Psr7\Response; | ||
| use Notur\Console\Commands\DevPullCommand; | ||
| use Notur\NoturServiceProvider; | ||
| use Orchestra\Testbench\TestCase; | ||
|
|
||
| class DevPullCommandTest extends TestCase | ||
| { | ||
| protected function getPackageProviders($app): array | ||
| { | ||
| return [NoturServiceProvider::class]; | ||
| } | ||
|
|
||
| protected function getEnvironmentSetUp($app): void | ||
| { | ||
| $app['config']->set('database.default', 'testing'); | ||
| $app['config']->set('database.connections.testing', [ | ||
| 'driver' => 'sqlite', | ||
| 'database' => ':memory:', | ||
| 'prefix' => '', | ||
| ]); | ||
| $app['config']->set('notur.repository', 'sak0a/notur'); | ||
| } | ||
|
|
||
| public function test_url_encodes_branch_names_with_slashes(): void | ||
| { | ||
| // Create a mock handler to capture HTTP requests | ||
| $container = []; | ||
| $history = Middleware::history($container); | ||
|
|
||
| $mock = new MockHandler([ | ||
| new Response(200, [], json_encode([ | ||
| 'sha' => 'abc123def456', | ||
| 'commit' => [ | ||
| 'message' => 'Test commit', | ||
| 'author' => [ | ||
| 'name' => 'Test Author', | ||
| 'date' => '2026-02-07T12:00:00Z', | ||
| ], | ||
| ], | ||
| ])), | ||
| ]); | ||
|
|
||
| $handlerStack = HandlerStack::create($mock); | ||
| $handlerStack->push($history); | ||
|
|
||
| // Test with a branch name containing slashes | ||
| $client = new Client(['handler' => $handlerStack]); | ||
|
|
||
| $command = new DevPullCommand(); | ||
| $reflection = new \ReflectionClass($command); | ||
| $method = $reflection->getMethod('fetchCommitInfo'); | ||
| $method->setAccessible(true); | ||
|
|
||
| $method->invoke($command, $client, 'sak0a/notur', 'feature/my-branch'); | ||
|
Comment on lines
+36
to
+64
|
||
|
|
||
| // Verify the URL was properly encoded | ||
| $this->assertCount(1, $container); | ||
| $request = $container[0]['request']; | ||
| $uri = (string) $request->getUri(); | ||
|
|
||
| // The branch name should be URL-encoded: feature/my-branch -> feature%2Fmy-branch | ||
| $this->assertStringContainsString('/commits/feature%2Fmy-branch', $uri); | ||
| $this->assertStringNotContainsString('/commits/feature/my-branch', $uri); | ||
| } | ||
|
|
||
| public function test_url_encodes_branch_names_with_special_characters(): void | ||
| { | ||
| // Create a mock handler | ||
| $container = []; | ||
| $history = Middleware::history($container); | ||
|
|
||
| $mock = new MockHandler([ | ||
| new Response(200, [], json_encode([ | ||
| 'sha' => 'xyz789abc', | ||
| 'commit' => [ | ||
| 'message' => 'Another test', | ||
| 'author' => [ | ||
| 'name' => 'Test Author', | ||
| 'date' => '2026-02-07T12:00:00Z', | ||
| ], | ||
| ], | ||
| ])), | ||
| ]); | ||
|
|
||
| $handlerStack = HandlerStack::create($mock); | ||
| $handlerStack->push($history); | ||
|
|
||
| $client = new Client(['handler' => $handlerStack]); | ||
|
|
||
| $command = new DevPullCommand(); | ||
| $reflection = new \ReflectionClass($command); | ||
| $method = $reflection->getMethod('fetchCommitInfo'); | ||
| $method->setAccessible(true); | ||
|
|
||
| // Test with a branch name containing spaces and special characters | ||
| $method->invoke($command, $client, 'sak0a/notur', 'feature/my branch-v2'); | ||
|
|
||
| $this->assertCount(1, $container); | ||
| $request = $container[0]['request']; | ||
| $uri = (string) $request->getUri(); | ||
|
|
||
| // The branch name should be URL-encoded | ||
| $this->assertStringContainsString('/commits/feature%2Fmy%20branch-v2', $uri); | ||
| } | ||
|
|
||
| public function test_handles_simple_branch_names_correctly(): void | ||
| { | ||
| // Create a mock handler | ||
| $container = []; | ||
| $history = Middleware::history($container); | ||
|
|
||
| $mock = new MockHandler([ | ||
| new Response(200, [], json_encode([ | ||
| 'sha' => '123abc456', | ||
| 'commit' => [ | ||
| 'message' => 'Simple test', | ||
| 'author' => [ | ||
| 'name' => 'Test Author', | ||
| 'date' => '2026-02-07T12:00:00Z', | ||
| ], | ||
| ], | ||
| ])), | ||
| ]); | ||
|
|
||
| $handlerStack = HandlerStack::create($mock); | ||
| $handlerStack->push($history); | ||
|
|
||
| $client = new Client(['handler' => $handlerStack]); | ||
|
|
||
| $command = new DevPullCommand(); | ||
| $reflection = new \ReflectionClass($command); | ||
| $method = $reflection->getMethod('fetchCommitInfo'); | ||
| $method->setAccessible(true); | ||
|
|
||
| // Test with a simple branch name without special characters | ||
| $method->invoke($command, $client, 'sak0a/notur', 'main'); | ||
|
|
||
| $this->assertCount(1, $container); | ||
| $request = $container[0]['request']; | ||
| $uri = (string) $request->getUri(); | ||
|
|
||
| // Simple branch names should still work | ||
| $this->assertStringContainsString('/commits/main', $uri); | ||
| } | ||
|
|
||
| public function test_handles_commit_sha(): void | ||
| { | ||
| // Create a mock handler | ||
| $container = []; | ||
| $history = Middleware::history($container); | ||
|
|
||
| $mock = new MockHandler([ | ||
| new Response(200, [], json_encode([ | ||
| 'sha' => 'abc123def456789', | ||
| 'commit' => [ | ||
| 'message' => 'SHA test', | ||
| 'author' => [ | ||
| 'name' => 'Test Author', | ||
| 'date' => '2026-02-07T12:00:00Z', | ||
| ], | ||
| ], | ||
| ])), | ||
| ]); | ||
|
|
||
| $handlerStack = HandlerStack::create($mock); | ||
| $handlerStack->push($history); | ||
|
|
||
| $client = new Client(['handler' => $handlerStack]); | ||
|
|
||
| $command = new DevPullCommand(); | ||
| $reflection = new \ReflectionClass($command); | ||
| $method = $reflection->getMethod('fetchCommitInfo'); | ||
| $method->setAccessible(true); | ||
|
|
||
| // Test with a commit SHA (no special characters) | ||
| $method->invoke($command, $client, 'sak0a/notur', 'abc123def456789'); | ||
|
|
||
| $this->assertCount(1, $container); | ||
| $request = $container[0]['request']; | ||
| $uri = (string) $request->getUri(); | ||
|
|
||
| // Commit SHAs should work as expected | ||
| $this->assertStringContainsString('/commits/abc123def456789', $uri); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test class extends Orchestra\Testbench\TestCase and boots NoturServiceProvider + DB config, but the tests only invoke a private method with a provided Guzzle client and don鈥檛 use the Laravel application/container. Consider switching to PHPUnit\Framework\TestCase (as most unit tests do) and removing getPackageProviders()/getEnvironmentSetUp() to reduce test runtime and avoid unnecessary framework coupling.